Producing multiple R graph files in a loop

5,707 views
Skip to first unread message

Li Xiaobo

unread,
Mar 21, 2012, 10:26:23 AM3/21/12
to ggplot2
hello!
 
I have a question you could give me some tips:
 
Assume I have 3 categories of data: and I would like to create a loop to have a scatter plot for each and the Title of the graph should contains “Category of plain”; I would like the code to be flexible so that for each category scatter plot, the program can give the name automatically. And also for the final graph, I would like to save into 3 files which has good visual effect in PPT presentation with the name of category: plain.bmp, river.bmp, and valley.bmp.
 
 
 
Thanks a lot!
Jack
 
category
x
y
plain
0.244655
1.582322
plain
0.227968
1.853089
plain
0.544354
1.69098
plain
0.430162
1.041818
plain
0.58876
1.541161
plain
0.591491
1.190874
plain
0.326573
1.432451
plain
0.377955
1.763906
plain
0.252851
1.10793
river
2.669902
3.511024
river
2.534036
3.722374
river
2.522919
3.857419
river
2.227972
3.451399
river
2.270864
3.445889
river
2.796426
3.734166
river
2.860347
3.398596
river
2.575079
3.029064
river
2.38188
3.494636
river
2.159525
3.229826
river
2.999258
3.956068
valley
10.08767
13.88812
valley
10.97654
13.39109
valley
10.34369
13.60074
valley
10.82515
13.53325
valley
10.91398
13.67478
valley
10.84971
13.41532
valley
10.29878
13.91667
valley
10.26964
13.77659
valley
10.98827
13.46049
valley
10.49848
13.84646
valley
10.52983
13.78469
valley
10.27991
13.99628
valley
10.67307
13.07919
valley
10.9463
13.54925
valley
10.92937
13.28916
valley
10.93894
13.44915
valley
10.34638
13.92315
valley
10.10787
13.88503
  

Raphael Saldanha

unread,
Mar 21, 2012, 10:39:07 AM3/21/12
to Li Xiaobo, ggplot2
Hi,

Assuming that you already have your data frame in R, you can try begin with something like this:

categories <- c("plain","river","valley")

for (i in categories)
{
  plot_data <- subset(data_frame, category == i)
  plot(plot_data)
}

Where data_frame represents your data frame.

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



--
Atenciosamente,

Raphael Saldanha
saldanha...@gmail.com
image003.png

Raphael Saldanha

unread,
Mar 21, 2012, 11:05:37 AM3/21/12
to Li Xiaobo, ggplot2
Here is a more complete solution:

library(ggplot2)

info <- read.csv("info.csv",header=TRUE)

categories <- unique(info$category)

for (i in categories)
{
  plot_data <- subset(info, category == i)
  qplot(x=x,y=y,data=plot_data, main=paste("Category of ",i,sep=""))
}

Where info is your data frame, loaded from a CSV file.
image003.png

Ben Bond-Lamberty

unread,
Mar 21, 2012, 11:07:50 AM3/21/12
to ggplot2
A final step, inside the loop, would be

>  ggsave(paste(i,"pdf",sep="."))   # or jpg or whatever

Ben

Raphael Saldanha

unread,
Mar 21, 2012, 11:10:56 AM3/21/12
to Ben Bond-Lamberty, ggplot2
Thanks Ben, I have forgot this. So, the code could be something like this:


library(ggplot2)

info <- read.csv("info.csv",header=TRUE)

categories <- unique(info$category)

for (i in categories)
{
  plot_data <- subset(info, category == i)
  plot <- qplot(x=x,y=y,data=plot_data, main=paste("Category of ",i,sep=""))
  ggsave(filename=paste("Category of ",i,".pdf",sep=""), plot=plot)
}

Before saving the plots, remember to set your working directory. Take a look on ?setwd

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

Matthew Krachey

unread,
Mar 21, 2012, 11:25:37 AM3/21/12
to Raphael Saldanha, ggplot2
If you have collaborators that use a lot of command line tools, such as Mac and Linux users, or if you automate importing or other operations on files (and folders) in R, you may want to save them a little bit of a headache and not put spaces in the file/folder name. It's a good habit to get into.

Matthew 
Reply all
Reply to author
Forward
0 new messages