使用 `rayshader` 包中的 `plot_gg()` 的问题 - R

Problems using `plot_gg()` from `rayshader` package - R

我正在尝试复制这里显示的示例,使用 rayshader 包制作: https://www.rayshader.com/reference/plot_gg.html

我特别关注直方图。下面我报代码:

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)

我的第一个问题是当我尝试制作 plot1 和 plot2 时出现以下错误:

Error in hillshade[, , 1] * scales::rescale(shadowmap, c(max_darken, 1)) :
arrays incompatible

我想了解为什么以及是否可以修复此错误。

第二个问题,工作中如何导出plot1plot2生成的图片?我尝试使用 ggsave() 的其他示例,但它不起作用。还有其他办法吗?

提前感谢您的支持。

也许这会有所帮助。 首先,要使“绘图”正常工作,如果要修改 height 参数,似乎还需要修改 width 参数。

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
            )

这里是第一个:

如果你想将它们保存为 .png,你正在使用正确的功能,但你必须打开 rgl window,即首先启动绘图,然后保存它。像这样:

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()

只需使用 GitHub 上 master 分支的最新版本再试一次。似乎这个问题已经注意到并在不久前解决了(参见 #176),但 CRAN 上尚未进行必要的更改。

## if package is missing, run `install.packages("remotes")` first
remotes::install_github(
  "tylermorganwall/rayshader"
)

library(rayshader)
library(ggplot2)

为了保存这两个图,您可以为 preview = TRUE 使用 built-in PNG 图形设备(您可能希望从 tempfile() 更改为更永久的东西):

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()

至于preview = FALSE(默认),像这样使用render_snapshot()

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
)