【问题标题】:Saving custom ggplot theme for future files为将来的文件保存自定义 ggplot 主题
【发布时间】:2021-08-17 10:05:40
【问题描述】:

抱歉,作为一个新手...在 R 中,我有一个自定义 ggplot 主题,我喜欢将其用于许多不同的项目(具有许多单独的脚本),但我发现需要将其复制并粘贴到每个项目中令人沮丧当我必须单独使用它们以避免炸毁我的笔记本电脑时编写脚本。

可能比较懒,但是有没有办法保存主题设置,这样当我打开一个新文件或创建一个新脚本时,我可以使用它而无需查找代码并重新复制它?

这是主题的缩短版:

    new_roz_theme <- theme_bw() +   
  theme(panel.border = element_rect(colour = "gray", size = 1.5), 
        panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  theme(plot.title = element_text(family = "Times New Roman",
                                  hjust=0.5, size = 12, face = "bold"),
        axis.title = element_text(family = "Times New Roman",size = 10),
        axis.text.x = element_text(family = "Times New Roman",size = 10),
        axis.text.y = element_text(family = "Times New Roman",size = 10),
        legend.title = element_text(family = "Times New Roman",size = 12),
        legend.text = element_text(family = "Times New Roman",size = 10),
        strip.background = element_rect(colour= "white", fill="white")) +
  theme(strip.background = element_blank(),
        strip.text.x = element_blank()) 

非常感谢您的任何建议! :)

【问题讨论】:

  • community.rstudio.com/t/… 和其中的警告。您可以通过使用base_sizebase_family 参数来简化您的主题theme_bw
  • 您可以使用saveRDS() 保存R 对象并使用readRDS() 调用它们。例如,saveRDS(new_roz_theme, "new_roz_theme.rds") 可以将它们保存到您当前的工作目录,new_roz_theme &lt;- readRDS("new_roz_theme.rds") 可以读取它们。为您的主题设置目录,您将始终知道它们在哪里。

标签: r ggplot2 plot customization


【解决方案1】:
library(ggplot2)
library(dplyr)

iris2 <- iris


## This is not generic version 

iris2 %>%
ggplot()+
geom_jitter(aes(x=Sepal.Width,y=Sepal.Length,color=Species))+theme_bw() +   
  theme(panel.border = element_rect(colour = "gray", size = 1.5), 
        panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  theme(plot.title = element_text(family = "Times New Roman",
                                  hjust=0.5, size = 12, face = "bold"),
        axis.title = element_text(family = "Times New Roman",size = 10),
        axis.text.x = element_text(family = "Times New Roman",size = 10),
        axis.text.y = element_text(family = "Times New Roman",size = 10),
        legend.title = element_text(family = "Times New Roman",size = 12),
        legend.text = element_text(family = "Times New Roman",size = 10),
        strip.background = element_rect(colour= "white", fill="white")) +
  theme(strip.background = element_blank(),
        strip.text.x = element_blank()) 


## assign the theme to an object

saved_theme <- theme_bw() +   
  theme(panel.border = element_rect(colour = "gray", size = 1.5), 
        panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  theme(plot.title = element_text(family = "Times New Roman",
                                  hjust=0.5, size = 12, face = "bold"),
        axis.title = element_text(family = "Times New Roman",size = 10),
        axis.text.x = element_text(family = "Times New Roman",size = 10),
        axis.text.y = element_text(family = "Times New Roman",size = 10),
        legend.title = element_text(family = "Times New Roman",size = 12),
        legend.text = element_text(family = "Times New Roman",size = 10),
        strip.background = element_rect(colour= "white", fill="white")) +
  theme(strip.background = element_blank(),
        strip.text.x = element_blank()) 

## save it as RDS
saved_theme %>% saveRDS('saved_theme.rds')




#delete the theme object
rm(saved_theme)


## read theme from file

themex <- readRDS('saved_theme.rds')


iris2 %>%
ggplot()+
geom_jitter(aes(x=Sepal.Width,y=Sepal.Length,color=Species))+
themex

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 2015-01-10
    • 2020-01-13
    • 2013-04-26
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    相关资源
    最近更新 更多