Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create plots in a loop using ggplot2

Tags:

r

ggplot2

I have to generate 250 plots with the same view.

My example data set:

df <- data.frame(name = c("john","alex","mike","dennis","alex"),
             expenses = c("10","12","15","8","2"),
             type = c("food","rent","rent","food","food"))

I would like bar plots with the expenses for every name in a single plot. The plot for "alex" will look like:

selected.name <- "alex"
df1 <- subset(df, name == selected.name)
ggplot(data = df1, aes(type, expenses)) + geom_bar()

Now I want to use a loop that plots the same plot for every name in the df. I have tried to use a for loop that runs the plot code above as a source file. But I can't pass the name variable to the source file so that it plots the graph for every name. Now I only get one graph out of the for loop.

like image 929
jeroen81 Avatar asked Feb 02 '12 11:02

jeroen81


People also ask

Does ggplot work in for loop?

r - ggplot does not work if it is inside a for loop although it works outside of it - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

What does Geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot. ggplot2 comes with many geom functions that each add a different type of layer to a plot.

How do I save a loop in ggplot2?

To save multiple ggplots using for loop, you need to call the function print() explicitly to plot a ggplot to a device such as PDF, PNG, JPG file. Enjoyed this article?


2 Answers

To answer your orignal question. To do this using standard R:

doPlot = function(sel_name) {
   dum = subset(df, name == sel_name)
   ggobj = ggplot(data = dum, aes(type, expenses)) + geom_bar()
   print(ggobj)
   ggsave(sprintf("%s.pdf", sel_name))
}
lapply(unique(df$name), doPlot)

In this way you end up with a large number of pdf files called Adam.pdf etc. You could then use pdftk (pdf tool kit) to cat the files together in one document. I would still prefer, a better solution using e.g. facetting or a different type of plot.

Wouldn't it be much better to use facetting? Given your example the code would be:

ggplot(data = df, aes(type, expenses)) + 
   geom_bar() + facet_wrap(~name)

which leads to the following plot:

enter image description here

Maybe for 250 names and more variables, this might be a problem. But I'd look at facetting nonetheless.

like image 93
Paul Hiemstra Avatar answered Sep 20 '22 11:09

Paul Hiemstra


A collegue just pointed out that using subset in a function is a very bad idea. See ?subset() for more info. So I adapted the anwser of Paul Hiemstra and replaced the subset.

doPlot = function(sel_name) {
   dum <- df[df$name == sel_name,]
   ggobj = ggplot(data = dum, aes(type, expenses)) + geom_bar()
   print(ggobj)
   ggsave(sprintf("%s.pdf", sel_name))
}
lapply(unique(df$name), doPlot)
like image 22
jeroen81 Avatar answered Sep 23 '22 11:09

jeroen81