1

I am trying to label a point in a 3D graph from 2D ggplot graph using rayshader. Here is what I have tried with render_label. I used save_height_matrix = TRUE to get the height matrix. The graph showed the label but in a wrong spot, and it should be on top of the highest point. Any suggestions would be appreciated.

library(ggplot2)
library(rayshader)
p <- ggplot(mtcars) + 
  geom_point(aes(x=mpg, y=disp, color=cyl)) + 
  scale_color_continuous(limits=c(0,8)) 

filename_movie = tempfile()
ht_matrix <- plot_gg(p, save_height_matrix = TRUE)
render_label(ht_matrix, text = "Pick this car", x = 10.4, y = 472, z = 9)
render_snapshot(filename = "fig_2.png")
rgl::rgl.close()

Edit

I have worked out a clumsy solution, show it here with a different dataset: Australian map.

#remotes::install_github("wfmackey/absmapsdata")
library(absmapsdata)
library(tidyverse)
library(ggthemes)
library(rayshader)
library(sf)
state_map <- absmapsdata::state2016

p <- state_map %>% 
  ggplot() +
  geom_sf(aes(geometry = geometry, fill = areasqkm_2016)) +
  theme_map() +
  scale_fill_viridis_c(option = "C")

filename_movie = tempfile()
ht_mx <- plot_gg(p,
                 multicore=TRUE,
                 width=5,
                 height=5,
                 scale = 350,
                 windowsize = c(1000, 1000),
                 phi = 30,
                 save_height_matrix = TRUE,
                 theta = 0
)
render_label(ht_mx, text = "Northern Territory", x = 760, y = 600, z =450)
render_label(ht_mx, text = "Queensland", x = 1000, y = 680, z =450)
render_snapshot(filename = "fig_1")
rgl::rgl.close()

Clumsy because x and y values in render_label are not longitude and latitude values. I just tried different numbers to work out the right ones.
Nevertheless, I got what I wanted.

enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27

0 Answers0