Saving two ggplots in grid.arrange to eps file format
Asked Answered
S

0

3

I am working in RStudio and have two ggplots, call them plot1 and plot2. I would like to arrange them one above the other and save into eps format. I have read the following posts on the topic:

Export a graph to .eps file with R

Saving grid.arrange() plot to file

Side-by-side plots with ggplot2

I have tried multiple methods which I list below, all of which have failed. Most revolve around using gridarrange.

1) My initial method was to run:

setEPS()
postscript("Figure1.eps",width=11.5,height=16)
grid.arrange(plot1,plot2,nrow=2,heights=c(9.5/20,10.5/20))
dev.off()

Similar to link 1 but with grid.arrange. There are no errors in R and an EPS file appears in my directory, however the filesize is small and I cannot open it in a file viewer, it appears to be corrupted for some reason.

2) My second attempt used ggsave:

grid.arrange(plot1,plot2,nrow=2,heights=c(9.5/20,10.5/20))
ggsave("Figure1.eps", p)

This saves only the second plot. Then try suggestions from comments from link 2:

The code

p <- arrangeGrob(plot1,plot2,nrow=2,heights=c(9.5/20,10.5/20))
grid.draw(p) # interactive device
ggsave("Figure1.eps", p)

gives output: Error in ggsave("saving.eps", p) : plot should be a ggplot2 plot

The code

grid.arrange(plot1,plot2,nrow=2,heights=c(9.5/20,10.5/20))
p<-arrangeGrob(plot1,plot2,nrow=2,heights=c(9.5/20,10.5/20))
ggsave("Figure1.eps", p)

gives output: Error in ggsave("saving.eps", p) : plot should be a ggplot2 plot

The code

grid.arrange(plot1,plot2,nrow=2,heights=c(9.5/20,10.5/20))
ggsave <- ggplot2::ggsave; body(ggsave) <- body(ggplot2::ggsave)[-2]
p<-arrangeGrob(plot1,plot2,nrow=2,heights=c(9.5/20,10.5/20))
ggsave("Figure1.eps", p)

gives output:

Saving 8.28 x 9.07 in image
TableGrob (2 x 1) "arrange": 2 grobs
  z     cells    name           grob
1 1 (1-1,1-1) arrange gtable[layout]
2 2 (2-2,1-1) arrange gtable[layout]

In this final case, a file appears in the directory but it is small and cannot be opened.

3) My final attempt is just to use the command

png("Figure1.eps",height=11.5,width=16,units="cm",res=600)
grid.arrange(plot1,plot2,nrow=2,heights=c(9.5/20,10.5/20))
dev.off()

This produces a 'eps' file and is of a large size, and can be opened, however I am unsure if to trust it. Why would the png command be able to create an eps file? I have a feeling it's just a png file but masked as an eps somehow if that is possible. Is there a way to check the files format?

EDIT: Code to create plots for which the above code can be ran.

data1<-data.frame(a=c(1,2,3,4,0.5,1,1.5,2,0.5,1,3,4),b=c(1,2,3,4,1,2,3,4,1,2,3,4),
      c=c('graph1','graph1','graph1','graph1','graph2','graph2','graph2','graph2','graph3','graph3','graph3','graph3'))

data2<-data.frame(a=c(10,20,25,40,13,14,25,37,2,20,34,35),b=c(1,2,3,4,1,2,3,4,1,2,3,4),
                  c=c('graph1','graph1','graph1','graph1','graph2','graph2','graph2','graph2','graph3','graph3','graph3','graph3'))

plot1<-ggplot(data1, aes(x=b,y=a)) +
  geom_line() +
  geom_point(size=2.5) +
  facet_wrap(~c, scales="fix") + 
  scale_x_continuous(limits=c(1,4),labels=c("Zero","Low","Med","High")) +
  ylab("ylab1") +
  coord_cartesian(ylim=c(0,4)) +
  theme(legend.position="none",axis.title.x=element_blank(), panel.margin.x=unit(1.1, "lines"))

plot2<-ggplot(data2,  aes(x=b,y=a)) +
  geom_line() +
  geom_point(size=2.5) +
  facet_wrap(~c, scales="fix") +
  scale_x_continuous(limits=c(1,4),labels=c("Zero","Low","Med","High")) +
  coord_cartesian(ylim=c(0,40)) +
  xlab("level of correlation") +
  ylab("ylab2") +
  theme(legend.position="none",axis.title.x=element_blank(), panel.margin.x=unit(1.1, "lines"))

EDIT: I have also now realised that

setEPS()
postscript("TestFig3.eps")
plot1
dev.off()

which should just simply create an eps file (no grid.arrange involved) also does not work. A file is created and no error message in R, but it is corrupted in some way.

Ideally I would like an

eps()
grid.arrange()
dev.off()

but ?eps provides no packages.

I realise there is already quite a lot of chat about this, however I have not been able to source a solution from it all.

Shaum answered 8/1, 2016 at 11:32 Comment(6)
Your third attempt definitely produces a PNG. Please provide a reproducible example.Riesman
Please see #5143342 and report whether it does what you needJounce
Hi Roland, I thought this was the case, when I right click and properties it says .eps trhough. I will provide a reproducible example for you. hrbrmstr, yes I have checked that link, it is the first link I provided and related to the first technique I used.Shaum
Reproducible example has been addedShaum
I cannot reproduce your problem on my system. postscript with setEPS produces an EPS. Are you sure that the problem isn't your viewer?Riesman
Yes quite sure. I have ran the same code but in R as opposed to R Studio and now it works, unsure of why that may be? Thank you for taking a look.Shaum

© 2022 - 2024 — McMap. All rights reserved.