在多个图中重用ggplot图层 [英] Reuse ggplot layers in multiple plots

查看:104
本文介绍了在多个图中重用ggplot图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制大量的图,这些图基本上使用相同的格式类型.只是想知道是否可以将这些图层存储在变量中并重用它们.

I am plotting tons of graphs which essentially use the same type of formatting. Just wondering if it possible to store these layers in a variable and reuse them.

方法1(无效)

t <- layer1() + layer2()
ggplot(df,aes(x,y)) + t

方法2(可行但不太优雅)

Approach 2 (works but not very elegant)

t <- function(x) x + layer1() + layer2()
t(ggplot(df,aes(x,y))

有任何方法1的建议吗?

Any suggestion along the lines of approach 1?

谢谢!

推荐答案

在我等待一些澄清的同时,以下示例演示了如何将先前创建的图层添加到现有绘图中:

While I wait for some clarification, here are a few examples that demonstrate how to add previously created layers to an existing plot:

p <- ggplot(mtcars,aes(x = cyl,y = mpg)) + 
        geom_point()    
new_layer <- geom_point(data = mtcars,aes(x = cyl,y = hp),colour = "red")
new_layer1 <- geom_point(data = mtcars,aes(x = cyl,y = wt),colour = "blue")

p + new_layer

p + list(new_layer,new_layer1)

这篇关于在多个图中重用ggplot图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆