r - 使用 ggplot2/Rstudio 进行缓慢的图形渲染 - GPU 问题?

标签 r windows ubuntu ggplot2

我正在使用 ggplot2 从包含大约 50 万行的表中制作图表。

在我的 ubuntu 20.04 笔记本电脑(CPU i5 8265U)上,它需要大约 15 秒并在 Rstudio 的绘图选项卡中正确显示。现在我刚买了一台 win 10 PC,更好的 CPU (i5 10400F) 和 GPU (GTX 1660 Super)。同一张图需要永远构建。如果我等得够久,代码会在超过 10 分钟后运行,但仍未显示在绘图选项卡中。

对我来说不幸的是,我无法共享数据,因此无法制作 reprex,图表的代码是:

> t1 <- Sys.time()
> gr_dbh_h <- tree16_temp %>%
+   filter(lu_en_simple2 %in% c("Evergreen Forest", "Deciduous Forest")) %>%
+   select(dbh, h, live_dead, lu_en_simple2_f) %>%
+   ggplot() +
+   geom_point(aes(x = dbh, y = h, color = live_dead), alpha = 0.5, shape = 3) +
+   labs(color = "", x = "Diameter at breast height (cm)", y = "Tree total height (m)") +
+   facet_wrap(~lu_en_simple2_f)
> t2 <- Sys.time()
> t2 - t1
Time difference of 0.1159708 secs
> gr_dbh_h
> t3 <- Sys.time()
> t3 - t2
Time difference of 9.901076 mins

不幸的是,最接近的 reprex 没有同样的问题,只是显示在 ubuntu 笔记本电脑上的渲染速度比 win10 PC 快 2 倍(而不是我的主代码中的 60 倍):

library(tidyverse)

tt  <- tibble(
  x = rnorm(500000),
  y = rnorm(500000),
  cat = rep(c("aa", "bb", "cc", "dd", "ee"), 100000),
  group = c(rep("A", 200000), rep("B", 300000))
)  
t1 <- Sys.time()
ggplot(tt) +
  geom_point(aes(x, y, color = group), alpha = 0.5) +
  facet_wrap(~cat)
t2 <- Sys.time()
t2 - t1

在 Ubuntu 上:

> t2 - t1
Time difference of 10.26094 secs

在 win10 上:

> t2 - t1
Time difference of 23.292 secs

所以主要问题是一个系统如何在 10 秒内制作图表而另一个系统制作时间超过 10 分钟?即使使用基本的图优先系统也能快 2 倍?

仅仅是 Ubuntu 与 Windows 的对比吗? GPU 会搞乱渲染吗?

Rstudio 和 R 是同一版本,在撰写本文时包都是最新的。

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_Europe.1252  LC_CTYPE=English_Europe.1252    LC_MONETARY=English_Europe.1252
[4] LC_NUMERIC=C                    LC_TIME=English_Europe.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggthemr_1.1.0   devtools_2.3.1  usethis_1.6.1   webshot_0.5.2   tmap_3.1        bookdown_0.20   knitr_1.29     
 [8] sf_0.9-5        BIOMASS_2.1.3   scales_1.1.1    ggrepel_0.8.2   ggpubr_0.4.0    lubridate_1.7.9 forcats_0.5.0  
[15] stringr_1.4.0   dplyr_1.0.2     purrr_0.3.4     readr_1.3.1     tidyr_1.1.2     tibble_3.0.3    ggplot2_3.3.2  
[22] tidyverse_1.3.0

最佳答案

欢迎使用糟糕的图形设备。在 OS X 上,有时保存为 pdf 然后在预览中打开 pdf 比将图形渲染到 RStudio 窗口更快。另外,here是一个文本渲染在一个图形设备中比在另一个图形设备中花费大约 300 倍的示例。

我建议安装 RStudio daily build , 安装ragg包,然后在图形设置中将后端设置为 agg:

enter image description here

这应该会给您带来不错的性能渲染。它还将修复默认 Windows 图形设备存在的一些其他问题,因此在任何情况下它都是一个不错的选择。

关于r - 使用 ggplot2/Rstudio 进行缓慢的图形渲染 - GPU 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63795842/

相关文章:

r - 在 amazon EC2 Windows 实例中从浏览器下载

r - 如何合并列联矩阵的两个输入值?

r - 不允许具有非零长度索引的零长度向量 -- 解释

r - ggplot 堆叠条的奇怪顺序

windows - 如何在 Windows 命令行中用不同的颜色回显

c# - IAP 不适用于 Win10Mobile 的 UWP 应用程序

ubuntu - 无法升级 NVidia 软件包

windows - 在Windows上使用Maven和Eclipse运行Hadoop 2.7.3

ruby - 无法在 Ruby 中使用 Gosu 打印文本

ubuntu - 不同用户的 Docker 镜像大小