save multiple within a loop using ggsave

2,379 views
Skip to first unread message

Matthias

unread,
Mar 22, 2011, 6:22:44 AM3/22/11
to ggplot2
We are trying to create and save multipe plots using ggplot (ggsave).

Here is an example of the problem we are currently not able to manage:

name<-c("a","b","c","d")
value<-1:4

dataframe<-data.frame(name,value)

leader<-max(dataframe$value)
meanvalue<-mean(dataframe$value)

for (i in 1:4) {

Index<-dataframe$value[i]

x<-c("leader", "meanvalue", dataframe$name[i])
y<-c(leader, meanvalue,Index)
plotframe<-data.frame(x,y)

z<-ggplot(plotframe, aes(x,y))+ geom_bar()

}



We want to plot each value for all 4 names compared to the mean and
the leader. We would like to get 4 plots to be saved in 4 different
files. We have tried savePlot, but it did not work.

Thanks in advance,

Matthias

Brandon Hurr

unread,
Mar 22, 2011, 7:20:53 AM3/22/11
to Matthias, ggplot2
Something like this should work...

for (i in 1:4) {

Index<-dataframe$value[i]

x<-c("leader", "meanvalue", dataframe$name[i])
y<-c(leader, meanvalue,Index)
plotframe<-data.frame(x,y)

z<-ggplot(plotframe, aes(x,y))+ geom_bar()
ggsave(sprintf("barplot.%s.png", i), width=6, height=6)
}

Brandon


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

Matthias

unread,
Mar 23, 2011, 11:49:26 AM3/23/11
to ggplot2
Thank you, Brandon!

It worked! You saved my day!
Reply all
Reply to author
Forward
0 new messages