从 ggplot 对象返回调用

发布于 2024-08-30 22:19:12 字数 1228 浏览 2 评论 0原文

我已经使用 ggplot2 一段时间了,但找不到从 ggplot 对象获取公式的方法。虽然我可以通过 summary() 获取基本信息,但为了获得完整的公式,通常我会通过 .Rhistory 文件进行上下梳理。当您尝试新图表时,这会变得令人沮丧,尤其是当代码变得有点长时......因此搜索历史文件并不是很方便的方法......有没有更有效的方法来做到这一点?只是一个例子:

p <- qplot(data = mtcars, x = factor(cyl), geom = "bar", fill = factor(cyl)) + 
     scale_fill_manual(name = "Cylinders", value = c("firebrick3", "gold2", "chartreuse3")) + 
     stat_bin(aes(label = ..count..), vjust = -0.2, geom = "text", position = "identity") + 
     xlab("# of cylinders") + ylab("Frequency") + 
     opts(title = "Barplot: # of cylinders")

我可以通过 summary 获取一些基本信息:

> summary(p)
data: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb [32x11]
mapping:  fill = factor(cyl), x = factor(cyl)
scales:   fill 
faceting: facet_grid(. ~ ., FALSE)
-----------------------------------
geom_bar:  
stat_bin:  
position_stack: (width = NULL, height = NULL)

mapping: label = ..count.. 
geom_text: vjust = -0.2 
stat_bin: width = 0.9, drop = TRUE, right = TRUE 
position_identity: (width = NULL, height = NULL)

但我想获取我输入的代码来获取图表。我认为我在这里遗漏了一些重要的东西......似乎不可能没有办法从 ggplot 对象调用!

I've been using ggplot2 for a while now, and I can't find a way to get formula from ggplot object. Though I can get basic info with summary(<ggplot_object>), in order to get complete formula, usually I was combing up and down through .Rhistory file. And this becomes frustrating when you experiment with new graphs, especially when code gets a bit lengthy... so searching through history file isn't quite convenient way of doing this... Is there a more efficient way of doing this? Just an illustration:

p <- qplot(data = mtcars, x = factor(cyl), geom = "bar", fill = factor(cyl)) + 
     scale_fill_manual(name = "Cylinders", value = c("firebrick3", "gold2", "chartreuse3")) + 
     stat_bin(aes(label = ..count..), vjust = -0.2, geom = "text", position = "identity") + 
     xlab("# of cylinders") + ylab("Frequency") + 
     opts(title = "Barplot: # of cylinders")

I can get some basic info with summary:

> summary(p)
data: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb [32x11]
mapping:  fill = factor(cyl), x = factor(cyl)
scales:   fill 
faceting: facet_grid(. ~ ., FALSE)
-----------------------------------
geom_bar:  
stat_bin:  
position_stack: (width = NULL, height = NULL)

mapping: label = ..count.. 
geom_text: vjust = -0.2 
stat_bin: width = 0.9, drop = TRUE, right = TRUE 
position_identity: (width = NULL, height = NULL)

But I want to get code I typed in to get the graph. I reckon that I'm missing something essential here... it's seems impossible that there's no way to get call from ggplot object!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耳钉梦 2024-09-06 22:19:12

目前不可能从 ggplot2 对象转到(可能)创建它的代码。

It's not currently possible to go from a ggplot2 object to the code that (might have) created it.

撩人痒 2024-09-06 22:19:12

您可以使用“expression()”将任何 R 代码存储为表达式,然后使用“eval()”对其求值。
例如

p <- expression(qplot(data = mtcars, x = factor(cyl), geom = "bar", fill = factor(cyl)) + 
     scale_fill_manual(name = "Cylinders", value = c("firebrick3", "gold2", "chartreuse3")) + 
     stat_bin(aes(label = ..count..), vjust = -0.2, geom = "text", position = "identity") + 
     xlab("# of cylinders") + ylab("Frequency") + 
     opts(title = "Barplot: # of cylinders"))

eval(p)

将生成绘图,但原始代码仍作为表达式存储在变量“p”中。

所以

p

产生的结果

expression(qplot(data = mtcars, x = factor(cyl), geom = "bar", 
    fill = factor(cyl)) + scale_fill_manual(name = "Cylinders", 
    value = c("firebrick3", "gold2", "chartreuse3")) + stat_bin(aes(label = ..count..), 
    vjust = -0.2, geom = "text", position = "identity") + xlab("# of cylinders") + 
    ylab("Frequency") + opts(title = "Barplot: # of cylinders"))

就是我们开始的结果。

如果使用 parse() 将字符串解析为文本,则“eval()”还可以将字符串作为表达式进行计算,例如

eval(parse(text='f(arg=value)')

You can store any R code as an expression with 'expression()' and then evaluate it with 'eval()'.
e.g.

p <- expression(qplot(data = mtcars, x = factor(cyl), geom = "bar", fill = factor(cyl)) + 
     scale_fill_manual(name = "Cylinders", value = c("firebrick3", "gold2", "chartreuse3")) + 
     stat_bin(aes(label = ..count..), vjust = -0.2, geom = "text", position = "identity") + 
     xlab("# of cylinders") + ylab("Frequency") + 
     opts(title = "Barplot: # of cylinders"))

then

eval(p)

will produce the plot but the original code is still stored in the variable 'p' as an expression.

so

p

produces

expression(qplot(data = mtcars, x = factor(cyl), geom = "bar", 
    fill = factor(cyl)) + scale_fill_manual(name = "Cylinders", 
    value = c("firebrick3", "gold2", "chartreuse3")) + stat_bin(aes(label = ..count..), 
    vjust = -0.2, geom = "text", position = "identity") + xlab("# of cylinders") + 
    ylab("Frequency") + opts(title = "Barplot: # of cylinders"))

which is what we started with.

'eval()' can also evaluate a character string as an expression if parsed as text with parse(), e.g.

eval(parse(text='f(arg=value)')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文