Plotting and saving PDFs in a loop

半世苍凉 提交于 2019-12-04 02:20:10

问题


I have a semi-melted data frame that looks like this:

head(final_melt)

   Group       Source variable   value
 Control Whole Kidney     MZF1 0.23879
 Control Whole Kidney     MZF1 0.49381
 Control Whole Kidney     MZF1 0.40827
 Control Whole Kidney     MZF1 0.55548
 Control Whole Kidney     MZF1 0.34664
 Control Whole Kidney     MZF1 0.68102

Group has two levels (Control and Disease), source has 4 levels (Whole Kidney, Glomerulus, Tubulointerstitium, and HK-2 + TGF-B). variable also has four levels (TFAP2A, MZF1, YY1, SP1). I would like to do something like the following in a loop

d = subset(final_melt, final_melt$Source=="Whole Kidney")
qplot(data=d, Group, value, facets=.~variable, geom="boxplot")
pdf("Whole Kideny.pdf")
dev.off()

While I know I could just say facets=Source~variable, the individual plots end up being too small to be informative. So I need to plot one level of the Source factor at a time.
The problem is I can't even get the pdf() function to work. It creates a file, with the correct name, but when I try to open it adobe says There was an error opening the file, and that it is already open in another application (why I added the dev.off(), but that didn't seem to do anything).

Any help is appreciated.
Cheers, Davy.


回答1:


  1. Open a graphics device (pdf(), png() etc.)
  2. Create and print your ggplot or lattice graphic.
  3. Call dev.off().
  4. .....?
  5. Profit.

In that order.




回答2:


As an alternative to joran's answer for ggplot2 graphics only:

  1. Create and print your ggplot graphic.
  2. ggsave(filename="Whole Kidney.pdf")

The ggsave will copy the last printed graphic. Or it can save a particular plot.

  1. Create your ggplot graphic and assign it to p
  2. ggsave(filename="Whole Kidney.pdf", p)


来源:https://stackoverflow.com/questions/9745675/plotting-and-saving-pdfs-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!