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
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)')
It's not currently possible to go from a ggplot2 object to the code that (might have) created it.