0

I am using the rayshader package to render a 3D plot with plot_gg() function. How can I customize or reduce the height of the scale triangle legend bar of the plot since it will cover the plot partly when we increase the argument scale (e.g. scale = 300, etc.).

My code is as following:

gg_mai = ggplot(mai_sff) +
  geom_sf(aes(fill = songuoi_km), lwd=0) +
  scale_fill_viridis("Mật độ người/km2", direction = -1, option = "viridis") +
  ggtitle("Họ Mai") +
  theme_bw()

plot_gg(gg_mai, multicore = TRUE, raytrace = TRUE, width = 7, height = 4, 
       scale = 300, windowsize = c(1400, 866), zoom = 0.6, phi = 30, theta = 30)

Thank you so much!

This is the output I have

Duc Du
  • 1
  • 1

1 Answers1

0

Well, you can define the key size in ggplot theme(). Without a reproducible example, I only added some arbitrary numbers. You may want to try different numbers on your side, even change the legend position.

gg_mai = ggplot(mai_sff) +
    geom_sf(aes(fill = songuoi_km), lwd=0) +
    scale_fill_viridis("Mật độ người/km2", direction = -1, option = "viridis") +
    ggtitle("Họ Mai") +
    theme_bw()+
    theme(
          legend.key.height = unit(2, "mm"),
          legend.key.width = unit(1, "mm")
    )      

plot_gg(gg_mai, multicore = TRUE, raytrace = TRUE, width = 7, height = 4, 
        scale = 300, windowsize = c(1400, 866), zoom = 0.6, phi = 30, theta = 30)
Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
  • Thanks! I've tried. But it actually only customize the height and the width of the legend key with respect to ggplot() function in 2D. It still does not work for the height of the scale in 3D. Does it have something to do to specify in plot_gg()? [It looks like this][1] [1]: https://i.stack.imgur.com/o74Qk.png – Duc Du Sep 15 '22 at 05:23
  • Try a smaller scale, like `scale = 100` – Zhiqiang Wang Sep 15 '22 at 06:35
  • Yes. It is have to trade-off with reducing the height of the bar inside the plot then as well that I don't want to... But that may be the only solution? Thank you. – Duc Du Sep 15 '22 at 06:52
  • Well, that's the purpose of the legend bar, to reflect data. This is more a data issue than code issue. You could try to re-scale `songuoi_km` variable, such as `mutate(songuoi_km) = ifelse(songuoi_km>=750, 750, songuoi_km)` – Zhiqiang Wang Sep 15 '22 at 07:21