Search code examples
rggplot2r-sfrayshader

add 2D image on 3D Plot RGL Device with rayshader in R


I want to add image on 3D plot which is shown on RGL Device. I try add image with annotation_raster() and annotation_custom() but image include be 3D, i expect it be flat. are there code can i use? Thanks..

I try this code but image include 3D, not flat.

image <-  magick::image_read("image.png")
image <- rasterGrob(image, interpolate = TRUE, 
                    width=unit(1.5,'cm'),
                    x = unit(1,"npc"), y = unit(1,"npc"),
                    hjust = 1, vjust=1)

map <- sf::st_read('map/map.shp', quiet = TRUE)
gg <- ggplot(map) +
   geom_sf()+
   geom_sf(aes(fill =AREA),linewidth=0.7,colour='black') +
   scale_fill_gradient('Area',low='skyblue',high = 'dodgerblue4',na.value = 'white')+
   annotation_custom(image)+
   theme_bw()+
   theme(axis.line = element_blank(),axis.title = element_blank(),
         axis.ticks = element_blank(), axis.text = element_blank()
         )
plot_gg(gg, height = 8.5, width = 9, 
        multicore = TRUE,windowsize = c(1050, 600),
        offset_edges = TRUE)

capture output my code


Solution

  • There could be some alternatives. One approach that I have used in my work is to use ggimage::geom_image(aes(x = 20, y = 30, image= "image.png"), size = 1) (adjust those numbers according to your data and original image size) to replace annotation_custom(image). This will give a 2D image instead of a 3D image on 3D graph. It worked on my machine with my data.

    If it does not work on your side, please provide your data (reproducible example) so I would be able to test it on my side.