【问题标题】:How to save a customize plot using ggsave with all customized properties?如何使用具有所有自定义属性的 ggsave 保存自定义绘图?
【发布时间】:2020-10-21 20:07:10
【问题描述】:

我有一个情节,然后我更改了ggplot 的一些默认属性,例如font face, font color, bold 等。但是,当我使用命令或Rstudio's plot 打开情节时,图像包含我所有的自定义属性。

但是,当我使用ggsave 保存绘图时,它没有显示和保存图像的所有自定义属性。更具体地说,我保存的图像没有显示them() 中给出的属性。

我的代码如下

plot <- ggplot(plot_df, aes(Region, diff, fill = Region))+
  geom_bar(stat = "identity", width = 0.8, position = position_dodge(width = 0.9))+
  theme_bw()+
  
  theme(panel.grid = element_blank(),
        plot.title = element_text(color = "black", face = "bold"),
        axis.text.x = element_text(colour="black",size=10,face="bold"),
        axis.text.y = element_text(colour="black",size=10,face="bold")
  )+
  xlab("Region")+
  ylab("Average Region Rainfall (mm)")+
  ggtitle("Average Region Ranfall") 

使用ggsave保存代码

ggsave("~/My/plot.png", plot = plot+
         theme_bw(base_size = 10, base_family = "Times New Roman"), width = 10, height = 8, dpi = 150, units = "in", device='png')

有没有办法使用 ggsave 保存原始图?

【问题讨论】:

  • 问题在于,通过保存plot+ theme_bw(base_size = 10, base_family = "Times New Roman"),您将使用 theme_bw 的默认值覆盖所有自定义主题参数。

标签: r ggplot2 plot ggsave


【解决方案1】:

只需将theme_bw() 更改为theme()。希望这会奏效。

ggsave("~/My/plot.png", plot = plot+
         theme(), width = 10, height = 8, dpi = 150, units = "in", device='png')

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 2022-07-12
    • 1970-01-01
    相关资源
    最近更新 更多