'Problems using `plot_gg()` from `rayshader` package - R
I am trying to replicate the example shown here, made with rayshader
package:
https://www.rayshader.com/reference/plot_gg.html
I was focused in particular on the histogram. Below I report the code:
library(ggplot2)
library(viridis)
library(rayshader)
library(tidyverse)
mtplot <- ggplot(mtcars) +
geom_point(aes(x=mpg,y=disp,color=cyl)) +
scale_color_continuous(limits=c(0,8))
mtplot
plot1 <- plot_gg(mtplot, width=3.5, sunangle=225, preview = TRUE)
plot2 <- plot_gg(mtplot, width=3.5, multicore = TRUE, windowsize = c(1400,866), sunangle=225,
zoom = 0.60, phi = 30, theta = 45)
render_snapshot(clear = TRUE)
My first problem is when I try to make plot1 and plot2 that I get the following error:
Error in hillshade[, , 1] * scales::rescale(shadowmap, c(max_darken, 1)) :
arrays incompatible
I would like to understand why and if it is possible to fix tis error.
My second question is, in case of work, how to export the image generated from plot1
and plot2
? I tried with other examples with ggsave()
but it is not working. Is there any other way?
Thank you in advance for every support.
Solution 1:[1]
Simply try again with the latest version from the master
branch on GitHub. It seems like the issue has been noticed and resolved a while ago (see #176), but the necessary changes are not yet on CRAN.
## if package is missing, run `install.packages("remotes")` first
remotes::install_github(
"tylermorganwall/rayshader"
)
library(rayshader)
library(ggplot2)
For saving the two plots, you can use the built-in PNG graphics device for preview = TRUE
(you will probably want to change from tempfile()
to something more permanent):
ofl1 = tempfile(fileext = ".png")
png(ofl1, width = 12, height = 8, units = "cm", res = 300)
plot1 = plot_gg(
mtplot
, width = 3.5
, sunangle = 225
, preview = TRUE
)
dev.off()
As for preview = FALSE
(default), use render_snapshot()
like this:
plot2 = plot_gg(
mtplot
, width = 3.5
, multicore = TRUE
, windowsize = c(1400, 866)
, sunangle = 225
, zoom = 0.60
, phi = 30
, theta = 45
)
ofl2 = tempfile(fileext = ".png")
render_snapshot(
filename = ofl2
, clear = TRUE
)
Solution 2:[2]
Maybe this could help.
First of all, to make "plots" working, if you want to modify the height
parameter, it seems you need to modify also the width
parameter.
library(ggplot2)
library(rayshader)
library(rgl)
mtplot <- ggplot(mtcars) +
geom_point(aes(x=mpg,y=disp,color=cyl)) +
scale_color_continuous(limits=c(0,8))
mtplot
plot_gg( mtplot
, width = 3.5
, height = 3.5
, sunangle = 225
)
plot_gg(mtplot
, width=3.5
, height = 3.5
, multicore = TRUE
, windowsize = c(1400,866)
, sunangle=225
, zoom = 0.60
, phi = 30
, theta = 45
)
Here the first:
If you want to save them as .png, you're using the right function, but you've to open the rgl window, i.e. first launch the plot, then save it. Something like this:
plot_gg( mtplot
, width = 3.5
, height = 3.5
, sunangle = 225
)
render_snapshot("path\\to\\your\\folder\\plot1.png",clear = TRUE)
# close rgl window
rgl.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | fdetsch |
Solution 2 |