3

I lost a couple of hours because of few transparent figures which Adobe Professional couldn't flatten to print (it crashed in the process).

I wonder if there's any trick to produce PDF figures with ggplot (I mean with the knitr option dev='pdf') which include transparent objects and yet avoid issues during pre-printing.

I don't know exactly why Acrobat Pro couldn't flatten them (yes, they are complicated) since it did it for other images. I have been trying these solutions (here and and here) but finally the only thing it worked was to convert the images to jpg before running LaTeX. And this is a pain because of course the text in the images is also converted.

The image I can't print with Acrobat Pro because it fails flattening it is something like (I avoided embedding the code into a knitr chunk, then this is pure R code):

require(ggplot2)
set.seed(123)
data = data.frame(date=seq(as.Date("2001-01-01"), as.Date("2014-01-01"), "weeks"),
                  value=rnorm(679))
mean0 = rnorm(1)
mean1 = rnorm(1)
mean2 = rnorm(1)
mean3 = rnorm(1)
mean4 = rnorm(1)

line1 = as.numeric(as.Date("2012-05-01"))
line2 = as.numeric(as.Date("2013-05-01"))
line3 = as.numeric(as.Date("2007-05-01"))

plot <- ggplot(data, aes(x=as.Date(date), y=value)) + 
  geom_line(size=.25, alpha=0.7) +
  xlim(as.Date("2001-01-01"), as.Date("2014-01-01")) +
  theme(legend.position="none") +
  geom_segment(y=0.26, x=line3-30, yend=0.24, xend=line3,
               arrow=arrow(length=unit(2, "mm")), alpha=.2, colour="gray") +
  geom_segment(y=0.20, x=line1-400, yend=0.165, xend=line1-20,
               arrow=arrow(length=unit(2, "mm")), alpha=.2, colour="gray") +
  geom_segment(y=0.28, x=line2-200, yend=0.30, xend=line2-20,
               arrow=arrow(length=unit(2, "mm")), alpha=.2, colour="gray") +
  geom_segment(y=0.1, x=as.numeric(as.Date("2004-01-01",origin="1970-01-01")), yend=0.02, xend=as.numeric(as.Date("2005-01-29",origin="1970-01-01")),
               arrow=arrow(length=unit(2, "mm")), alpha=.2, colour="gray") +
  annotate("text", label="Text", y=0.1, x=as.Date("2004-01-01",origin="1970-01-01"), size=4) +
  annotate("text", label="Text", y=0.265, x=as.Date(line3,origin="1970-01-01")-30, size=4) +
  annotate("text", label="Text", y=0.20, x=as.Date(line1,origin="1970-01-01")-500, size=4) +
  annotate("text", label="Text", y=0.275, x=as.Date(line2,origin="1970-01-01")-300, size=4) +
  labs(y="Frequency",x=NULL) +
  geom_segment(aes(y=mean0, x=as.Date("2001-01-01",origin="1970-01-01"), yend=mean0, xend=as.Date("2005-01-29",origin="1970-01-01"))) +
  geom_segment(aes(y=mean1, x=as.Date("2005-01-29",origin="1970-01-01"), yend=mean1, xend=as.Date(line3,origin="1970-01-01"))) +
  geom_segment(aes(y=mean2, x=as.Date(line3, origin="1970-01-01"), yend=mean2, xend=as.Date(line1, origin="1970-01-01"))) +
  geom_segment(aes(y=mean3, x=as.Date(line1, origin="1970-01-01"), yend=mean3, xend=as.Date(line2, origin="1970-01-01"))) +
  geom_segment(aes(y=mean4, x=as.Date(line2, origin="1970-01-01"), yend=mean4, xend=as.Date("2014-01-01", origin="1970-01-01")))

pdf("nonprintable_plot.pdf", width = 4, height = 4)
grid.arrange(plot, ncol=1)
dev.off()
  • I've hit this recently with a plot with multiple geom_paths and transparency. It's not a big plot, but the flattener chokes on it. Strangely, a larger version of the plot with more paths flattened ok. – mikeck Apr 23 '18 at 22:44
  • You can add the + theme(rect = element_rect(fill = "transparent") ) option. stackoverflow.com/questions/7455046/… – skan May 15 '18 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.