从 ggplot 对象返回调用
我已经使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前不可能从 ggplot2 对象转到(可能)创建它的代码。
It's not currently possible to go from a ggplot2 object to the code that (might have) created it.
您可以使用“expression()”将任何 R 代码存储为表达式,然后使用“eval()”对其求值。
例如
,
eval(p)
将生成绘图,但原始代码仍作为表达式存储在变量“p”中。
所以
p
产生的结果
就是我们开始的结果。
如果使用 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.
then
eval(p)
will produce the plot but the original code is still stored in the variable 'p' as an expression.
so
p
produces
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)')