Creating graphs from using a loop from ggplot2

4,294 views
Skip to first unread message

Vivek

unread,
Apr 17, 2012, 7:52:24 PM4/17/12
to ggplot2
Hi All:

I want to create graphs from ggplot2 using a loop. For instance I
have 4 columns of measured data in iris dataset. I want to write a
loop that will create histograms of these columns faceted by the
Species (5th column of data).

My code looks like this, but I am getting errors.

library(ggplot2)
data(iris)
for (i in 1:4) {
chartTitle <- colnames(iris)[i];
p <- ggplot() + geom_histogram(aes(x = iris[,i]),data=iris) +
facet_grid(facets = Species ~ .) + opts(title = chartTitle);
print(p);
}

Any help would be greatly appreciated.

Vivek

Michael Griffiths

unread,
Apr 17, 2012, 8:00:29 PM4/17/12
to Vivek, ggplot2
Use ggsave() in your loop

http://svitsrv25.epfl.ch/R-doc/library/ggplot2/html/ggsave-ao.html

Hi All:

Vivek

--
You received this message because you are subscribed to the ggplot2 mailing
list.
Please provide a reproducible example:
https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

baptiste auguie

unread,
Apr 17, 2012, 8:02:05 PM4/17/12
to Vivek, ggplot2
Hi,

You can use aes_string,

library(ggplot2)
data(iris)
for (i in 1:4) {
chartTitle <- colnames(iris)[i];

p <- ggplot() + geom_histogram(aes_string(x = chartTitle),data=iris) +


facet_grid(facets = Species ~ .) + opts(title = chartTitle);

dev.new() ; print(p);
}


or melt the data into long format first,

library(reshape2)
library(plyr)

d_ply(melt(iris, id="Species"), "variable", function(d) ggplot() +
geom_histogram(aes(x = value), data=d) +
facet_grid(facets = Species ~ .) , .print=TRUE)


HTH,

baptiste

Vivek

unread,
Apr 17, 2012, 9:08:02 PM4/17/12
to ggp...@googlegroups.com
Thanks Baptiste, both work wonderfully!


On Tuesday, April 17, 2012 7:02:05 PM UTC-5, baptiste wrote:
Hi,

You can use aes_string,

library(ggplot2)
data(iris)
for (i in 1:4) {
 chartTitle <- colnames(iris)[i];
 p <- ggplot() + geom_histogram(aes_string(x = chartTitle),data=iris) +
facet_grid(facets = Species ~ .) + opts(title = chartTitle);
 dev.new() ; print(p);
}


or melt the data into long format first,

library(reshape2)
library(plyr)

d_ply(melt(iris, id="Species"), "variable", function(d) ggplot() +
geom_histogram(aes(x = value), data=d) +
facet_grid(facets = Species ~ .) , .print=TRUE)


HTH,

baptiste

Vivek

unread,
Apr 18, 2012, 12:01:12 AM4/18/12
to ggp...@googlegroups.com
I can make histograms, but when I try to make density plots the aes_string does not work.  Here is my code

data(iris)
for (i in 1:4) {
  chartTitle <- colnames(iris)[i];
  p<- ggplot() + geom_density(aes_string(x = chartTitle,y = ..density..,colour = as.factor(Species)),data=iris) + opts(title = chartTitle);
  print(p)
}

ERROR

Error in aes_string(x = chartTitle, y = ..density.., colour = as.factor(Species)) : 
  object '..density..' not found


I tried placing the ..density.. in quotes and did not work. 

Vivek

Dennis Murphy

unread,
Apr 18, 2012, 2:03:36 AM4/18/12
to Vivek, ggp...@googlegroups.com
Hi:

..density.. is returned from stat_density() while the ggplot is being
processed; see http://had.co.nz/ggplot2/stat_density.html. As a
result, you can't use it as a quoted argument in aes_string(), since
all aesthetics defined there come from the input data frame in one way
or another (e.g., looping over a character vector of variable names
belonging to the input data frame). If you use the approach that
Baptiste showed you with d_ply(), substituting geom_density() for
geom_histogram() should work. Using melt() to combine variables for
plotting is an important tool in learning to use ggplot2 effectively,
as is d_ply() for processing plots groupwise.

To simplify the problem, write the plot code into a function that
takes a generic data frame as its argument and then call it within
d_ply(), something like (tested)

gplot <- function(d) {
print(ggplot(d, aes(x = value)) +
geom_density(aes(color = Species)) +
opts(title = d$variable) +
facet_grid(Species ~ .) )
}

d_ply(melt(iris, id = 'Species'), 'variable', gplot)

HTH,
Dennis

Vivek

unread,
Apr 19, 2012, 10:58:08 PM4/19/12
to ggp...@googlegroups.com
Thanks, this solved the problem.

Vivek
Reply all
Reply to author
Forward
0 new messages