for (i in xxx) ggplot problem
Posted
by Andreas
on Stack Overflow
See other posts from Stack Overflow
or by Andreas
Published on 2010-04-01T23:32:50Z
Indexed on
2010/04/02
0:23 UTC
Read the original article
Hit count: 258
This is strange - I think?
library(ggplot2)
tf <- which(sapply(diamonds, is.factor))
diamonds.tf <- diamonds[,tf]
So far so good. But next comes the trouble:
pl.f <- ggplot(diamonds.tf, aes(x=diamonds.tf[,i]))+
geom_bar()+
xlab(names(diamonds.tf[i]))
for (i in 1:ncol(diamonds.tf)) {
ggsave(paste("plot.f",i,".png",sep=""), plot=pl.f, height=3.5, width=5.5)
}
This saves the plots in my working directory - but with the wrong x-label. I think this is strange since calling ggplot directly produces the right plot:
i <- 2
ggplot(diamonds, aes(x=diamonds[,i]))+geom_bar()+xlab(names(diamonds)[i])
I don't really know how to describe this as a fitting title - suggestions as to a more descriptive question-title is most welcome.
Thanks in advance
© Stack Overflow or respective owner