【问题标题】:ggplot2: plot saved as pdf is different from displayer plotggplot2:保存为 pdf 的绘图与显示绘图不同
【发布时间】:2022-10-01 14:19:08
【问题描述】:

我正在尝试以 pdf 格式保存两个 ggplots。当我在 r 绘图显示器中可视化它们时,一切都很好。保存 png 或其他图像格式时,保存的图像正是我在 r 显示器中可视化的图像。但是,当尝试另存为 pdf 时,结果却大不相同。

我用了: dev.print(pdf,\'Viral_load_methods.pdf\')

对于其中一个图,误差条变得非常细,如下所示。

原版 enter image description here

pdf版本 enter image description here

对于另一个图,箱线图的线条变得很粗。

原始显示 enter image description here

pdf显示 enter image description here

如何获得以 pdf 格式保存的、忠实于 r 显示的绘图?

  • 试试ggplot2::ggsave(),它通常更适合ggplot2 对象

标签: r ggplot2 pdf


【解决方案1】:

正如有人在 cmets 中指出的那样,ggsave 是一个可自定义的选项以及绘图窗口。例如,此代码将绘制 iris 数据集,然后将其保存为具有我想要的尺寸的 pdf:

#### Load Tidyverse ####
library(tidyverse)

#### Make Plot ####
iris %>% 
  ggplot(aes(x=Petal.Length,
             y=Petal.Width))+
  geom_point()+
  geom_smooth(method = "lm")

#### Save Plot as PDF ####
ggsave("StackPlot.pdf",
       width = 10, # controls width
       height = 6) # controls weight

之后您可以在您的目录中找到它。这是我的:

【讨论】:

    猜你喜欢
    • 2019-03-28
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    相关资源
    最近更新 更多