【发布时间】: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_size和base_family参数来简化您的主题theme_bw -
您可以使用
saveRDS()保存R 对象并使用readRDS()调用它们。例如,saveRDS(new_roz_theme, "new_roz_theme.rds")可以将它们保存到您当前的工作目录,new_roz_theme <- readRDS("new_roz_theme.rds")可以读取它们。为您的主题设置目录,您将始终知道它们在哪里。
标签: r ggplot2 plot customization