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
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
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
Error in aes_string(x = chartTitle, y = ..density.., colour = as.factor(Species)) :
object '..density..' not found
..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