Jos*_*chi 4 optimization r ggplot2 knitr
我想在ggplot2中外化我的布局,因为我的代码变得非常拥挤.
可以说我有一个给定的布局,我应用于不同功能的几个图.它看起来像这样:
scale_fill_gradientn(guide="colourbar",colours=costum.colorbar_sand.blue.green)+
theme (legend.title = element_text(size = 15,family="Arial"))+
theme (legend.text = element_text(size = 12,family="Arial",angle=45))+
theme (legend.position = "bottom")+
coord_map(projection="mercator");
Run Code Online (Sandbox Code Playgroud)
我想把它放在外部文件或对象中,然后将其调用到我的绘图函数中.
我尝试了knitr包中的 read_chunk :我将空白代码保存在R文件中,然后尝试在我的函数中实现它.因此我只是把它写成这样的函数
function.xy(...){...some ggplot function...
read_chunk("some\path")
}
Run Code Online (Sandbox Code Playgroud)
但它没有成功.我也尝试过,paste(read_chunk("some\path"))但这不会起作用.
我的另一个想法是将它保存为一个对象而只是粘贴文本,但这不太合适,因为我不知道如何将它保存为纯文本对象(如果有类似的东西) .任何人都可以建议我如何做到这一点?
我这样做有两种方式.在我要编织的文件中,我声明了我知道将经常使用的绘图选项,将分组元素保存在列表中,如下所示:
plot.option1 <- list(scale_fill_gradient(guide="colourbar",colours=costum.colorbar_sand.blue.green),
theme(legend.title = element_text(size = 15,family="Arial"),
legend.text = element_text(size = 12,family="Arial",angle=45),
legend.position = "bottom"),
coord_map(projection="mercator"))
Run Code Online (Sandbox Code Playgroud)
然后,这只是一个附加的问题 + plot.option1
例如:
ggplot(dat, aes(x,y)) + plot.option1
Run Code Online (Sandbox Code Playgroud)
如果你有很多(比如公司主题),你可以很容易地将它们放在一个单独的脚本中,然后在你文件的头部将它作为编织源.
最小的例子:
plot.opt <- list(scale_color_brewer(palette="Blues"))
ggplot(diamonds, aes(carat, price, color=color)) + geom_point() + plot.opt
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
386 次 |
| 最近记录: |