Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ggplot2 is there a way to retrieve the commands used to make a plot in R? [duplicate]

Tags:

r

ggplot2

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!

like image 317
aL3xa Avatar asked May 04 '10 11:05

aL3xa


People also ask

What is the name of the function in ggplot2 that you use to start every plot by creating a coordinate system to which laters may be added?

With ggplot2, you begin a plot with the function ggplot() . ggplot() creates a coordinate system that you can add layers to.

Is ggplot2 same as Ggplot?

You may notice that we sometimes reference 'ggplot2' and sometimes 'ggplot'. To clarify, 'ggplot2' is the name of the most recent version of the package. However, any time we call the function itself, it's just called 'ggplot'.

What is the difference between plot and Ggplot in R?

Base R plots two vectors as x and y axes and allows modifications to that representation of data whereas ggplot2 derives graphics directly from the dataset. This allows faster fine-tuning of visualizations of data rather than representations of data stitched together in the Base R package1.

What is the AES in the ggplot2 system?

Aesthetic Mapping ( aes ) In ggplot2 , aesthetic means “something you can see”. Each aesthetic is a mapping between a visual cue and a variable. Examples include: position (i.e., on the x and y axes) color (“outside” color)


2 Answers

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

like image 173
hadley Avatar answered Nov 06 '22 05:11

hadley


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

like image 22
wkmor1 Avatar answered Nov 06 '22 07:11

wkmor1