-1

I am trying to export different figures with corresponding file names.

For example, there is a table called 'A' and it has following structure.

 V1      V2 
 chr1     0.9
 chr1     0.1
 chr1     0.2
 chr2     1.1
 chr2     1.3
 chr2     0.1

plot(A[which(A$V1==chr1),2])
plot(A[which(A$V1==chr2),2])
.
.
.
.

output files

chr1.pdf
chr2.pdf

I succeeded in making different plots with different titles by V1 column. However, I cannot make different names of files.

Give me your nice solutions

narendra-choudhary
  • 4,582
  • 4
  • 38
  • 58
Sejin
  • 85
  • 9
  • Can you please elaborate on what you are trying to plot by using a MWE http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – David Nov 15 '15 at 10:06
  • David, I also want to explain precisely what I want to do. However, I have no idea, how to draw or upload scheme about my situation. I want to make different plot according to V1 column values and then export them into pdf format with names of V1 values – Sejin Nov 15 '15 at 10:17
  • Have you looked into `ggplot2` and its `ggsave`-function? You are then able to use a `for`-loop to create and save the plot... – David Nov 15 '15 at 10:24
  • David. Thank you for your suggestion. I will read ggplot2 manual. – Sejin Nov 15 '15 at 14:07

1 Answers1

1

Use a for loop:

for (v in unique(A$V1)){
  plot(A[which(A$V1) == v, 2])
  dev.copy2pdf(paste0(v, ".pdf"))
}
Hugh
  • 15,521
  • 12
  • 57
  • 100