Using a font from extrafont in grid.draw
Asked Answered
P

1

6

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

enter image description here

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!

Photodrama answered 1/1, 2017 at 5:12 Comment(4)
Try "TradeGothic LT CondEighteen"Lemmie
Interesting. Isn't that what I tried? How is it different?Photodrama
DId you do extrafont::loadfonts()?Lemmie
Yeah, I did. And, I mean, the font works. It's only when I try to use grid.draw() with it that it breaks.Photodrama
L
7

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:

library(purrr)

loadfonts()

pdfFonts() %>% 
  map_chr("family") %>% 
  keep(~grepl(".*Trade.*", .))
## [1] "TradeGothic LT CondEighteen"

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 referencing plot vs p and actually grid.draw()ing gt vs plot or p after modifying the ggplot gtable:

library(ggplot2)
library(grid)
library(extrafont)

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

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

loadfonts()

ggsave('plot.pdf', plot=p,  path="~/Desktop")

That ^^ part makes the following PDF (exported from Preview as a PNG):

enter image description here

gt <- ggplot_gtable(ggplot_build(p))
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(1, max(gt$layout$r))

pdf("~/Desktop/plot.pdf")
grid::grid.draw(gt)
dev.off()

That ^^ part makes the following PDF (exported from Preview as a PNG):

enter image description here

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="TradeGothicLT-CondEighteen"))

gt <- ggplot_gtable(ggplot_build(p))
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(1, max(gt$layout$r))

grid::grid.draw(gt)

Here's a screen grab from RStudio (with bits of RStudio UI included to show it's in an RStudio graphics device):

enter image description here

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.

Lemmie answered 1/1, 2017 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.