I'm trying to add a white background to a 2D plot with the rayshader R package.
The approach I'm taking is to create an image with 255 values and then use the add_overlay function.
Here is the code I have:
Read in a tif file (which includes NA values surrounding the area of interest):
dem_sp <- terra::rast("dem_image_with_nas.tif")
Replace NAs with 255 to create white image
dem_sp_255 <- terra::subst(dem_sp, NA, 255)
Convert raster to matrix
v_mat_nas <- raster_to_matrix(dem_sp)
v_mat_255 <- raster_to_matrix(dem_sp_255)
Create ambient shade object
ambmat <- ambient_shade(v_mat_nas, zscale = 50)
Create basemap:
basemap <- v_mat_nas %>%
add_overlay(height_shade(v_mat_255), alphalayer = 0.5) %>%
sphere_shade(texture = "imhof2") %>%
add_shadow(ambmat, max_darken = 0.6)
This is the error:
Error in newarray[, , 1] <- image_overlay :
number of items to replace is not a multiple of replacement length
I can't find many consistent examples on now to add images with the add_overlay function and very little on adding a white background to a 2D plot. For example, I'm unclear on the syntax for adding 'hillshade' to the add_overlay function as I want add the white-coloured image as background. Maybe there is a better way to do this?
Thanks for your advice.