Export to pdf not displaying properly in ggplot2 - r

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.

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.

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).

Related

Specific fonts in ggplot2 not saving to pdf error

I'm trying to save multiple plots to one pdf, typically I use:
# creating pdf from four panels
# these aren't reproducible but any ggplot will recreate error essentially
pdf(file = "Fig.1", height = 14, width = 14)
Fig.1<- grid.arrange(p1,p2,p3,p4)
print(Fig.1)
dev.off()
However, I want to use a specific font in the plots.
I have thus downloaded and installed the fontextrapackage, intalled and loaded the fonts with loadfonts(device = "win"). I then use theme(text=element_text(family="Calibri")) within the ggplot call.
When I re-run code individual plots will look fine in R itself. I can also successfully use grid.arrange using gridExtra to arrange them and check them within R itself. However, when I use:
pdf(file = "Fig.4.trw.pdf", height = 14, width = 14)
Fig.4.trw<- grid.arrange(p1,p2,p3,p4)
I get a bunch of errors associated to fonts:
In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
I've also tried to use ggsave but that isn't working either, please help!
Thanks

Embedded pdf-font in R-plot is not recognized by InDesign although available

Using ggplot2, extrafont and R's pdf device I have built plots in the cmyk colormodel that incorporate certain non-Windows fonts. The pdf-output looks fine and shows that the font has been embedded correctly, for instance "Arial-BoldMT".
Unfortunately, when trying to import the pdf into Adobe InDesign, I get the error message that the font "Arial-BoldMT" is not currently available, which also happens to the non-Windows fonts I mentioned above.
I suppose there might be a problem with the name of the embedded font that cannot be recognized by InDesign, since the font is very well available as "Arial" including all the variations such as "bold".
Any suggestions how to get those fonts working in InDesign by either adjusting the R script or using InDesign?
Thank you!
Here is a sample plot, similar to the plots I need to produce, just leaving out the unnecessary code lines:
library(ggplot2)
library(extrafont)
# define font
font <- "Arial"
# sample data
x <- data.frame(c("Personnel Costs", "Non-Personnel Costs", "Investments"),
c(33, 22, 45))
colnames(x) <- c("costs", "percent")
# plot
plot <- ggplot(x, aes("", y = percent, fill = factor(costs), width = 1.2))+
geom_bar(width = 4, stat="identity")+
# add the text (no font specification here)
geom_text(aes(label=costs),fontface = "bold")+
# no legend
theme(legend.position = "none") +
# pie-chart
coord_polar("y", start = 0.42, direction = 1)
# save plot
pdf("plot.pdf", family=font, colormodel="cmyk")
plot
dev.off()
PS: Using ggsave and embedFonts() with Ghostscript produced the same results.
Note: Using "Calibri" with the pdf device or ggsave and embed_fonts() does not work at all.
It seems if the font was not properly embedded into the pdf.
By running embed_fonts() after saving the plot, the according font got embedded and now works in InDesign.
I just needed:
library(extrafont)
embed_fonts(file="plot.pdf", outfile="plot.pdf")

Saving ggplot graph to PDF with fonts embedded in r

I've been following advice I've found online for saving a ggplot graph to PDF but I can't quite get it to work. I'm using the extrafont package to produce charts with text in Calibri, but my charts are printing out with no text. I don't know what I'm missing. I can't find any mistakes in my process. Hopefully, someone else can help.
Here's the code and process I used:
library(extrafont)
font_import(pattern="[C/c]alibri")
loadfonts(device="win")
I installed GhostScript at this time. Then ran the following to set the GhostScript location.
Sys.setenv(R_GSCMD = "C:\\Program Files\\gs\\gs9.21\\bin\\gswin64c.exe")
I then produced a chart using ggplot called "chart". The chart looked perfect in RStudio, but not in PDF.
ggsave("chart.pdf", plot = chart, width = 6, height = 4)
Here I get warnings showing stuff like this:
In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... : font family 'Calibri' not found in PostScript font database
Apparently, these warnings are supposed to happen? Then I run...
embed_fonts("chart.pdf", outfile="chart_embed.pdf")
Unfortunately, after all this, the final "embed" chart looks no different than the original chart produced, neither of which have any text.
In case it helps, here's the code to produce the chart:
a <- ggplot(data=stats, aes(x=Date))
Chart <- a + geom_point(aes(y=NevadaTotalNonfarmAllEmployees)) +
xlab("Date") +
ylab("Nonfarm Jobs") +
ggtitle("Nevada Total Jobs") +
theme(axis.title.x = element_text(size=15, family = "Calibri"),
axis.title.y = element_text(size=15, family = "Calibri"),
axis.text.x = element_text(size=10, family = "Calibri"),
axis.text.y = element_text(size=10, family = "Calibri"),
plot.title = element_text(hjust=0.5, size=20, family = "Calibri"))
I've been pulling my hair out trying to figure this out. Or maybe it's not the code but something else? Either way, thanks for any assistance.
There are a couple issues at play here: (1) loading fonts into R and (2) using a PDF-writing library that works correctly with custom embedded fonts.
First, as others have mentioned, on Windows you generally need to run extrafont::font_import() to register many of your system fonts with R, but it can take a while and can miss TTF and other types of fonts. One way around this is to load fonts into R on the fly, without loading the full database, using windowsFonts(name_of_font_inside_r = windowsFont("Name of actual font")), like so:
windowsFonts(Calibri = windowsFont("Calibri"))
This makes just that one font accessible in R. You can check with windowsFonts(). You have to run this line each time the script is run—the font loading doesn't persist across sessions. Once the font has been loaded, you can use it normally:
library(tidyverse)
df <- data_frame(x = 1:10, y = 2:11)
p <- ggplot(df, aes(x = x, y = y)) +
geom_point() +
labs(title = "Yay Calibri") +
theme_light(base_family = "Calibri")
p
Second, R's built-in PDF-writing device on both Windows and macOS doesn't handle font embedding very well. However, R now includes the Cairo graphics library, which can embed fonts just fine. You can specify the Cairo device in ggsave() to use it, which is easier than dealing with GhostScript:
ggsave(p, filename = "whatever.pdf", device = cairo_pdf,
width = 4, height = 3, units = "in")
I’ve found it safer to explicitly register fonts using pdfFonts (and/or postscriptFonts).
The documentation contains an example but also take a look at my fonts module. With this, registering a new font is as easy as writing
fonts$register_font('Calibri')
Internally, this creates a font specification using Type1Font, ensures that names are set correctly, and invokes pdfFonts.
It also ensures that the complete set of font metrics to exist (which is done using extrafont::ttf_import).
This way is considerably faster than generating font metrics for all fonts using font_import, and it gives you more control.
I think you missed the initialization step font_import(). Be forewarned, executing this command can take a bit longer time.
First, you can see what fonts you have available with the command windowsFonts(). The current fonts in my graphing device are;
$serif
[1] "TT Times New Roman"
$sans
[1] "TT Arial"
$mono
[1] "TT Courier New"
Thereafter, you can import the extrafont library and the loadfonts(device = "win"). I also recommend to execute these commands in the R console and not in RStudio. I suggest this because when you are importing the fonts using font_import() in RStudio, it may not show the y/n prompt.
Below I provide a minimum reproducible example;
library(ggplot2)
library(extrafont)
font_import()
# tell where ghostscript is located. This is required for saving the font in pdf
Sys.setenv(R_GSCMD = "C:\\Program Files\\gs\\gs9.21\\bin\\gswin64c.exe") # I have installed 64-bit version of GhostScript. This is why I've used gswin64c.exe. If you have installed 32-bit version of GhostScript, use gswin32c.exe. Failure to specify the correct installed GhostScript will yield error message, "GhostScript not found"
# create a plot object
p <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
ggtitle("Fuel Efficiency of 32 Cars")+
xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
theme_bw()+
theme(text=element_text(family="ArialMT", size=14))
# show the plot
print(p)
# save the plot as pdf
ggsave("figures//ggplot_arialmt.pdf", p, width=20, height=20,
device = "pdf", units = "cm")
Note
Its only the ArialMT font that seems to work with ggsave(). See this SO post. Using any other font for saving to pdf, renders the figure with characters on top of another. This is also an open issue for ggsave and has not been answered since 2013.

pdf font and graph formatting (win.graph)

I recently came upon a problem in R which in that combination I have tried to solve and also searched for in the internet, but which I could not solve yet. I hope someone can help me.
I run R(x64) on Windows 7. The graphic device itself automatically uses Arial as font, and when I save graphs as bitmap the "font" naturally remains as it is. However, I prefer saving graphs as pdf, in which case the font in the resulting pdf is exchanged with Helvetica when I simply save via the GUI save button.
I found a solution for that in the internet, using the Arial afm-Files and pdf("Test_Auto.pdf", family = "Arial"), which resulted in a pdf using Arial as font---so far so good.
Now I also often have to/want to change the graph layout using win.graph, and that is where the problems start. Here is an example:
Arial <- Type1Font(family = "Arial", metrics = c("C:/R_Fonts/ArialPlain.afm", "C:/R_Fonts/ArialBold.afm", "C:/R_Fonts/ArialItalic.afm", "C:/R_Fonts/ArialBoldItalic.afm"))
pdfFonts(Arial = Arial)
setwd("C:/PDFCrop")
D1<-matrix(c(1,2,3,4,6,3),3,2)
D2<-matrix(c(1,2,3,5,3,1),3,2)
#pdf("Test_Auto.pdf", family = "Arial")
win.graph(8.3,12,12)
layout(matrix(c(1,2),1,2,byrow=TRUE))
plot(D1,type="l",main="Gobble R")
plot(D2,type="l",main="Gobble R")
#dev.off()
Now this code as it is works to create a graph that looks like I want it to look, but I have to save the graph manually (File->save as) and then I get Helvetica as font in the pdf.
Alternatively I can change the lower part in
pdf("Test_Auto.pdf", family = "Arial")
#win.graph(8.3,12,12)
layout(matrix(c(1,2),1,2,byrow=TRUE))
plot(D1,type="l",main="Gobble R")
plot(D2,type="l",main="Gobble R")
dev.off()
And that produces a pdf-file that actually uses Arial, but the graph has other dimensions as I was intending. When using both together I get a pdf which "cannot be opened because it does't contain any pages" (though it is not 0KB in size).
Is there any way to get this to work, or an alternative to win.graph which I can use between pdf() and dev.off()?
Thanks for you help.
Oh my god, I'm sorry, I was so stupid!
During all that I completely overlooked, that the pdf-device has its own size parameters.
Arial <- Type1Font(family = "Arial", metrics = c("C:/R_Fonts/ArialPlain.afm", "C:/R_Fonts/ArialBold.afm", "C:/R_Fonts/ArialItalic.afm", "C:/R_Fonts/ArialBoldItalic.afm"))
pdfFonts(Arial = Arial)
setwd("C:/PDFCrop")
D1<-matrix(c(1,2,3,4,6,3),3,2)
D2<-matrix(c(1,2,3,5,3,1),3,2)
pdf("Test_Auto.pdf", width=8.3, height=12, family = "Arial")
#win.graph(8.3,12,12)
layout(matrix(c(1,2),1,2,byrow=TRUE))
plot(D1,type="l",main="Gobble R")
plot(D2,type="l",main="Gobble R")
dev.off()
Thank you for the hint, DWin

R eps export and import into Word 2010

I'm having trouble with exporting eps files from R and importing into Word 2010.
I'm using ggplot2 plots, eg
library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth()
p
Even after calling setEPS() neither of the following produce files which can be successfully imported.
ggsave("plot.eps")
postscript("plot.eps")
print(p)
dev.off()
The strange thing is that if I produce the plot using File -> Save As -> Postscript from the menu in the GUI, it can be imported correctly. However, when the Word document is subsequently exported as a pdf, the fonts in the graphic are a little jagged.
So my questions are:
What combination of (ggsave/postscript) settings allows me to produce eps files that can be imported into Word 2010?
How can I ensure the fonts remain clear when the Word document is exported as a pdf?
Update
After more investigation I have had more luck with cairo_ps to produce the plots. However, no text shows up when imported into Word.
Furthermore, after checking the various eps outputs (cairo_ps, save from the GUI, ggsave) in a latex document, it seems like the eps import filter in Word quite poor as the printed/pdf output doesn't match the quality of the latex'd document. The ggsave version (which uses postscript) did have some issues with colours that the other two methods didn't have though.
The conclusion is that this is a Word issue and therefore fortune(109) does not apply. I'd be happy to be proven otherwise, but I'll award the answer and the bounty to whoever can provide the commands that can replicate the output from the GUI in command form.
This worked for me... following advice in the postscript help page:
postscript("RPlot.eps", height = 4, width = 4, horizontal = FALSE, onefile = FALSE,
paper = "special")
library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth()
p
#geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to #change the smoothing method.
#Warning message:
#In grid.Call.graphics(L_polygon, x$x, x$y, index) :
# semi-transparency is not supported on this device: reported only once per page
dev.off()
#quartz
# 2
The funny stuff at the end puts you on notice that this is only a Mac-tested solution, so far anyway.
Edit: I just tested it with R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows": Platform: i386-pc-mingw32/i386 (32-bit) and MS Word 2007 in Win XP and it worked. Commands were Insert/Picture.../select eps format/select file.
Edit2: There is another method for saving besides directly using the postscript device. The savePlot method with an "eps" mode is available in Windows (but not in the Mac). I agree that the fonts are not as smooth as they appear on a Mac but I can discern no difference in quality between saving with savePlot and using save as from an interactive window.
savePlot(filename = "Rplot2", type = "eps", device = dev.cur(), restoreConsole = TRUE)
savePlot calls (.External(CsavePlot, device, filename, type, restoreConsole))
I solved the problem with exporting .eps files from R and importing into Word 2010 on Windows 7 using the colormodel="rgb" option (defaults to "srgb") of the postscript command.
postscript("RPlot.eps", height = 4, width = 4, horizontal = FALSE,
paper = "special", colormodel = "rgb")
library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth(se=FALSE, method="loess")
p
dev.off()
You are probably better of using wmf as a format which you can create on Windows.
Word indeed doesn't support EPS very well.
A better solution is to export your graphs to Word or Powerpoint directly in native Office format. I just made a new package, export, that does exactly that, see
https://cran.r-project.org/web/packages/export/index.html and
for demo
https://github.com/tomwenseleers/export
Typical syntax is very easy, e.g.:
install.packages("export")
library(export)
library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species,
size = Petal.Width, alpha = I(0.7))
graph2doc(file="ggplot2_plot.docx", width=6, height=5)
graph2ppt(file="ggplot2_plot.pptx", width=6, height=5)
Output is vector format and so fully editable after you ungroup your graph in Word or Powerpoint. You can also use it to export statistical output of various R stats objects.
You can use R studio to knit html files with all of your plots and then open HTML files with Word.
knitr tutorial

Resources