问题
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:
- Open a graphics device (
pdf()
,png()
etc.) - Create and print your ggplot or lattice graphic.
- Call
dev.off()
. - .....?
- Profit.
In that order.
回答2:
As an alternative to joran's answer for ggplot2
graphics only:
- Create and print your ggplot graphic.
ggsave(filename="Whole Kidney.pdf")
The ggsave
will copy the last printed graphic. Or it can save a particular plot.
- Create your ggplot graphic and assign it to
p
ggsave(filename="Whole Kidney.pdf", p)
来源:https://stackoverflow.com/questions/9745675/plotting-and-saving-pdfs-in-a-loop