【问题标题】:How to reduce the size of a ggplot figure? The plot has too many lines and thus weights too much如何减小ggplot图形的大小?情节线太多,因此权重太大
【发布时间】:2020-07-10 20:51:44
【问题描述】:

我正在绘制一项模拟研究的结果,其中我已经重复了 500 次某个分析。我想展示一个数量如何在模拟(意大利面条图)中随时间变化,以显示模拟中的异质性。但是,行数过多,情节变得沉重(在pdf中使用ggssave)。有什么建议如何在不损失太多分辨率的情况下获得更轻的图片?

下面是代码和图片示例

ggplot(sim_data, aes(month, val, group = sim)) +
  geom_line(col = "grey") +
  facet_wrap(upper_cap_letter ~ lower_cap_letter)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我写了一个我posted on github 的光栅化函数,它可以帮助减小文件大小。渲染图形需要更长的时间,但文件大小可能会更小。

    您可以通过适当地设置dpi 来控制所需的分辨率。

    # Script gives the rasterise function
    source("https://gist.githubusercontent.com/teunbrand/c3f5d2d1a8e94294ddeed1d2f22687d2/raw/6ebe4c2f801191fc1195ee8d28dccb97373db14c/rasterising_ggplot_layers.R")
    #> Warning: package 'ragg' was built under R version 4.0.2
    
    # 125000 observations
    df <- expand.grid(1:50, LETTERS[1:5], letters[1:5], 1:100)
    df$y <- runif(nrow(df))
    
    
    ggplot(df, aes(Var1, y, group = Var4)) +
      rasterise(geom_line(), dpi = 300) +
      facet_wrap(~ Var2 + Var3)
    

    reprex package (v0.3.0) 于 2020 年 7 月 10 日创建

    注意:我将函数写到add into the ggraster 包中,所以如果你在一段时间后看到这篇文章,它可能就在那里。

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 2023-04-04
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 2018-12-01
      • 2018-10-08
      相关资源
      最近更新 更多