R 从ggplot对象返回调用

R 从ggplot对象返回调用,r,ggplot2,R,Ggplot2,我使用ggplot 2已经有一段时间了,我找不到从ggplot对象获取公式的方法。虽然我可以通过summary()获得基本信息,但为了得到完整的公式,我通常是通过.rhistore文件进行上下梳理。当您尝试使用新图形时,这会变得令人沮丧,尤其是当代码变得有点长时。。。所以在历史文件中搜索并不是很方便的方法。。。有没有更有效的方法?仅举一个例子: p <- qplot(data = mtcars, x = factor(cyl), geom = "bar", fill = factor(cy

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

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")

但是我想得到我输入的代码来得到图表。我想我错过了一些重要的东西。。。似乎不可能从
ggplot
对象获取调用

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

这就是我们开始的

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


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

当前无法从ggplot2对象转到(可能)创建它的代码。

我查看了
p
str()
dput
,我找不到调用。可能不可能。使用脚本文件如何?如果您使用的是R-GUI,则可以轻松使用它。例如,文件->新建打开一个新的空白脚本文件。不保存它,您可以计算脚本文件(在mac、command-e或command-enter中).kohske,我使用的是GNU/Linux发行版,Emacs+ESS,我担心没有任何GUI…至少没有“常规GUI”…但我不清楚的是:“你对
脚本文件是什么意思?
”我提到了一种从
.rhistore
文件获取调用的方法,但这不是我要寻找的…我会在
ggplot2
组上试试运气…这显然是一个方便的功能…马特…是的,没有运气…一堆对象,但是
调用
就是不存在!我的意思是如果你使用脚本文件而不是控制台,哟您可以轻松地在脚本文件中获取上一个调用,而无需从.rhistore中检索。当然,您可以在ESS环境中使用脚本文件。这不是解决您问题的方法,而是一种解决方法。我同意您的看法,如果ggplot2对象具有调用,则很方便。当然,我可以,字符串(字符)也应该足够了。类似这样的内容:
s当然可以。但是如果ggplot没有将您的输入保存到它返回的对象中,那么类似这样的内容将是您唯一的选择。@aL3xa.按照您的建议添加了另一种使用eval()的方式。我一直在等待您的建议!=)您认为这是个好主意吗?我当然希望在新版本中实现此功能…它在我的待办事项列表中,但不是很高,因为不幸的是,它需要大量重写:/
> 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)
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"))
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"))