Check out aes_string.
Hadley
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
Is there a reason you don't want to use a data.frame in the long format?
As an example, you could do,
library(ggplot2)
library(gridExtra)
GOP <- read.csv("http://dl.dropbox.com/u/3734701/GOP.csv")
mgop <- melt(GOP, id="Time")
str(mgop)
commonopts <- opts(plot.title=theme_text(size=9),
axis.text.x=theme_text(size=6),
axis.text.y=theme_text(size=6),
axis.title.y=theme_text(angle=90,size=8),
axis.title.x=theme_text(size=8),
axis.ticks=theme_blank())
## function to generate one plot
plotone <- function(var, ...){
p = ggplot(subset(mgop, variable==var), aes(x=Time,y=value)) +
geom_line(size=0.05,colour="blue") +
labs(x="", y="") +
opts(title=var) +
commonopts
}
## loop over the variables and store all plots (5 in the example)
allplots <- llply(levels(mgop$variable)[1:5], plotone)
## optional, add parameters to be passed to arrange
plotsandoptions <- c(allplots, list(main="All the plots"))
do.call(arrange, plotsandoptions)
(I couldn't figure out what you were trying to do with the axes limits)
HTH,
baptiste
aes(x=Time,y=GOP[,cols[i]]])
The aesthetic mapping should contain variable names, not values. You
should instead do:
aes_string(x = "Time", y = cols[i])
Hadley
But it's so easy to do it without a layout method:
plotsome <- function(var, ...){
ggplot(subset(mgop, variable %in% var), aes(x=Time,y=value)) +
geom_line(size=0.05,colour="blue") +
facet_wrap(~ variable) +
labs(x="", y="") +
opts(title=var) +
theme_grey(8)
}
levels <- levels(mgop$variable)
letter <- substr(levels, 1, 1)
allplots <- llply(split(levels, letter), plotsome)
# OR
l_ply(split(levels, letter), plotsome, .print = T)
And that way you have total flexibility.
Hadley
Definitely don't do this - it will work in simple cases, but will
invoke R's recycling rules in more complex cases, so errors may be
silently ignored. The preferred way is to create a new data frame and
then use the data argument to the layer.
Hadley
Add dev.new() before each call
> I would also like to save each output to an image file as it is
> cycling through the groups and plotting them.
Add ggsave() after each call.
I was trying to work through this older post written by you from July,
and I was unable to get it to run. Any suggestions on what I may be
doing incorrectly. The error I get is:
> do.call(arrange, plotsandoptions)
Error in rank(x, ties.method = "min", na.last = "keep") :
unimplemented type 'list' in 'greater'
Nick (the OP) had sent me a new data link, and your example is pasted
below my sessionInfo.
> sessionInfo()
R version 2.11.1 (2010-05-31)
i386-pc-mingw32
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United
States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United
States.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets
methods base
other attached packages:
[1] gridExtra_0.7 ggplot2_0.8.8 proto_0.3-8 reshape_0.8.3 plyr_1.1
### start example
GOP <- read.csv("http://dl.dropbox.com/u/3734701/GOP.csv")
library(ggplot2)
library(gridExtra)
mgop <- melt(GOP, id="Time")
commonopts <- opts(plot.title=theme_text(size=9),
axis.text.x=theme_text(size=6),
axis.text.y=theme_text(size=6),
axis.title.y=theme_text(angle=90,size=8),
axis.title.x=theme_text(size=8),
axis.ticks=theme_blank())
## function to generate one plot
plotone <- function(var, ...){
p = ggplot(subset(mgop, variable==var), aes(x=Time,y=value)) +
geom_line(size=0.05,colour="blue") +
labs(x="", y="") +
opts(title=var) +
commonopts
}
## loop over the variables and store all plots (5 in the example)
allplots <- llply(levels(mgop$variable)[1:5], plotone)
## optional, add parameters to be passed to arrange
plotsandoptions <- c(allplots, list(main="All the plots"))
do.call(arrange, plotsandoptions)
as Dennis said, arrange() was renamed grid.arrange() after plyr
introduced its own arrange() function (unrelated, but which throws the
confusing error message you got).
Best,
baptiste