Export to pdf not displaying properly in ggplot2
Asked Answered
I

2

3

I have a complex figure P made up of figures Fig_NPK and Barchart_fert, they were made using a datasheet "Fert" with columns including "Vil", "N", "P", and "K". My goal is to create a pdf file and use ghostscript to embed the "Times New Roman" font family in the pdf file. When I use ggsave to export "P" to a pdf file it doesn't display properly. The code used were:

library(extrafont)
library(ggplot2)

Figures, Fig_N as an example (Fig_N, P, and K form Fig_NPK)

Fig_N<-ggplot(aes(y = N, x = factor(Vil)), data = Total_fac[which(Total_fac$N<quantile(Total_fac$N,0.95)&Total_fac$N>quantile(Total_fac$N,0.05)),])+stat_boxplot(geom ="errorbar") + geom_boxplot() +ggtitle("N requirement")+labs(x="Village",y="Amount used (kg)") +theme(text=element_text(family="Times New Roman", face="plain", size=14))
Fig_NPK<-plot_grid(Fig_N,Fig_P,Fig_K, nrow=3,align = "v")
Barchart_fert<-ggplot(Fert, aes(x=Village, y=Amount, fill=Fertilizer)) + geom_bar(stat = "identity", width=0.4)+ggtitle("Fertilizer usage")+ylab("Amount used (kg)")+theme(axis.text.x=element_text(vjust = 0.5))+scale_fill_discrete(name="Fertilizer type", breaks=c("N-Eq.", "P2O5-Eq.", "K2O-Eq."),labels=c("N Eq.", expression('P'[2]*'O'[5]~~Eq.), expression('K'[2]*'O'~~Eq.)), c=60, l=80)+theme(text=element_text(family="Times New Roman", face="plain", size=14))
P<-ggdraw() + draw_plot(Fig_NPK,0,0,.4,1)+draw_plot(Barchart_fert,.4,0,.6,1) + draw_plot_label(c("A", "B"), c(0,.4), c(1,1), size=14)
ggsave("FigP.pdf", plot=P, width=5, height=5)

Error message after ggsave:

Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,  : Metric information not available for this family/device In addition: There were 50 or more warnings (use warnings() to see the first 50)

The resulting pdf file is incomplete. The same thing happens when I try exporting "P" to a postscript file. I have been following the help file and other answers on embedding fonts but can't seem to solve my issue.

Ioneionesco answered 27/11, 2016 at 7:25 Comment(2)
Follow up question. Whenever I try exporting these figures in R Studio (to postscript for instance), error appears saying that postscript device does not have "Times New Roman" and the export window freezes up with "converting plot" at the top, at which time R Studio command line stops working, ESC and force break don't work either, and I would have to close R Studio at the Windows command console. I am running R Studio Version 0.99.486 on Windows 10, with R version 3.2.2.Ioneionesco
Another follow up, tried 'loadfonts(device="win")' in a previous question link, still failed to export to postscript or pdf files. However, I have worked around the non-responsive issue by calling ggsave directly instead of using the R Studio's export command window, this way only error message would appear but the execution will end.Ioneionesco
I
4

Found a solution in this answer. Adding

device=cairo_pdf

in ggsave does solve the problem of not display the correct font in pdf and tiff files, though as @user1092247 pointed out, the kerning seems awkward. Would still appreciate it if anyone could perfect this solution, and explain a bit more on what the problem actually was.

Ioneionesco answered 30/11, 2016 at 6:27 Comment(2)
Quick and easy solution. I had no problem exporting all sorts of fonts before, but am facing this problem now with a clean install of MacOS Sierra. I had to increase the width and height of the plots inside ggsave, apparently the cairo_pdf device exports at slightly different sizes. (increasing width from 7 to 10 was enough to rescale the font to about right)Mccafferty
Thanks, seems to have improved the scales a bit.Ioneionesco
A
1

Indirect way- Save the image as png in R-studio. Covert png to pdf using other software (such as inkspace - change settings in Document properties).

Antemeridian answered 24/5, 2017 at 3:57 Comment(2)
Add plotly package and then it will save as pdfAntemeridian
Thanks. Seems to work, though the display of the new PDF file is slightly different from what I would get out of a generic one. However, in my case, I was trying to build a PDF file (containing vector graphics and embedded font) fitting editorial requirements of a journal. Going through a raster png might be problematic.Ioneionesco

© 2022 - 2024 — McMap. All rights reserved.