Say I have a dataset like this:
dat <- data.frame
text = c(
"It made me feel very positive to brand X",
"It was clear and easy to understand",
"I didn't like it al all"),
value=runif(3)
)
I can plot it in ggplot using can the TradeGothic LT CondEighteen font from the extrafonts
package:
library(ggplot2)
p <- ggplot(dat, aes(text, value)) +
geom_bar(stat="identity") +
coord_flip() +
labs(title=" Do you agree with the following statements?")+
theme_bw(16)+
theme(text=element_text(family="TradeGothic LT CondEighteen"))
ggsave('plot.pdf', plot = plot, path = "/Users/jacobdeecurtis/Desktop")
But when I use ggplot_gtable
on the plot:
gt <- ggplot_gtable(ggplot_build(plot))
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(1, max(gt$layout$r))
grid::grid.draw(plot)
ggsave('plot.pdf', plot = plot, path = "/Users/jacobdeecurtis/Desktop")
I get an error once I run the grid.draw function. The error is:
Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
polygon edge not found
In addition: Warning messages:
1: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
no font could be found for family "TradeGothic LT CondEighteen"...
I don't get the error when I don't use the TradeGothic LT CondEighteen font.
Thanks for your help!
I, uh, "just happened to have" a copy of this font and did the
extrafont::font_import()
dance and got your error. But, upon further inspection:it seems the PDF device wants this name.
While R has a host of awesomeness, the way it deals with fonts is about as nuanced and friendly and usable as a lizard crow (#atla).
UPDATE
Complete example (without the broken
data.frame
creation code and referencingplot
vsp
and actuallygrid.draw()
inggt
vsplot
orp
after modifying the ggplot gtable:That ^^ part makes the following PDF (exported from Preview as a PNG):
That ^^ part makes the following PDF (exported from Preview as a PNG):
Here's a screen grab from RStudio (with bits of RStudio UI included to show it's in an RStudio graphics device):
It's sad that we have to do this (change naming conventions for the font so various bits of R presentation code can pick the right one) but that's the current state of fonts in R.
I generate content for research reports for my company and separate theme code for screen (during development) and production PDF generation (for final output handoff to the creative team). It's a frustrating crutch, but it works.