【问题标题】:Slow graph rendering with ggplot2 / Rstudio - GPU issue?使用 ggplot2 / Rstudio 进行缓慢的图形渲染 - GPU 问题?
【发布时间】:2020-09-08 14:16:28
【问题描述】:

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

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

不幸的是,我无法共享数据,因此无法进行代表,图表的代码是:

> 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

【问题讨论】:

    标签: r windows ubuntu ggplot2


    【解决方案1】:

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

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

    这应该会给您提供不错的性能渲染。它还将修复默认 Windows 图形设备存在的一些其他问题,因此无论如何它都是一个不错的选择。

    【讨论】:

    • 谢谢,我听从了你的指示,得到了更好的表现!小问题是我曾经用 ggsave 以 cm 为单位保存我的地块,但不知何故它不受支持(请参阅github.com/r-lib/ragg/issues/45)另外仅供参考,我在我的 PC 上的 WSL 上安装了 Rstudio 服务器,并在 3 秒内得到了我的地块,没有 ragg,所以Win10 Rstudio渲染图有问题。
    猜你喜欢
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多