0

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.

Peter
  • 11,500
  • 5
  • 21
  • 31
GiacomoDB
  • 369
  • 1
  • 10

2 Answers2

3

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
)
fdetsch
  • 5,239
  • 3
  • 30
  • 58
1

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:

enter image description here

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()
s__
  • 9,270
  • 3
  • 27
  • 45
  • If omitted, `width` and/or `height` are set to `3` (units) by default, see `?plot_gg`. There is no need for an explicit specification. – fdetsch May 13 '22 at 11:50
  • 1
    @fdetsch OP seems to need to change the default values and I've seen that if you'd like to change `width` you've to change anso `height`. Regard your answer, I got the CRAN version, and explicit both parameters makes it work. – s__ May 13 '22 at 11:52
  • 1
    True, just tested with the CRAN release. So in summary, either use the CRAN version and specify both `width` and `height` (or don't specify them at all, in which case the defaults are assumed) as you suggest. Or use the GitHub version as in my answer, in which case you can specify one argument and simply omit the other. I figure both answers are valid then (: – fdetsch May 13 '22 at 12:12
  • Thank you, it works the firt part. The second concerning the export of the images it generates a file of 54 kb. Is it possible to increase the resolution? Maybe also zoom in to the graph? – GiacomoDB May 13 '22 at 12:14
  • @GiacomoDB it seems a known issue. You can find some info [here](https://stackoverflow.com/questions/58797147/increase-resolution-of-r-rayshader-image). I've tried [this](https://github.com/tylermorganwall/rayshader/issues/28) without any luck. – s__ May 13 '22 at 12:35
  • I saw that topic, I was wondering if anything new came up. – GiacomoDB May 13 '22 at 12:40
  • 1
    @GiacomoDB The easiest way to increase the resolution of the image is to set the `windowsize` parameter in `plot_gg()`. E.g. `windowsize = c(1000,800)`. @s__ You can set a width and height parameter independent of the screen resolution in `render_snapshot()` if you set `software_render = TRUE`. – Tyler May 14 '22 at 03:29