Using font from extrafont in grid.draw

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 build it in ggplot using 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 chart:

 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 when I run the grid.draw function. Error:

 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 do not get an error when I do not use the font TradeGothic LT CondEighteen.

Thank you for your help!

+5
source share
1 answer

I, uh, just got a copy of this font and did an extrafont::font_import() dance and got your error. But upon further verification:

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

The PDF device seems to need this name.

While R has a lot of awesomeness, the way it deals with fonts is about as nuanced, friendly, and helpful as the raven lizard (#atla).

UPDATE

Full example (without broken code to create data.frame and link plot vs p and actually grid.draw() ing gt vs plot or p after changing 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") 

This part ^^ does the following PDF (exported from Preview as 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() 

This part ^^ does the following PDF (exported from Preview as 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 you can capture the screen from RStudio (with the RStudio UI bits turned on for display in the RStudio graphics device):

enter image description here

It’s sad that we have to do this (change the naming conventions for the font so that the different bits of the R view code can choose the right one), but that the current state of the fonts is in R.

I create content for research reports for my company and a separate theme code for the screen (during development) and the production of PDF generation (for the final transfer of data to the creative team). This is a frustrating crutch, but it works.

+6
source

Source: https://habr.com/ru/post/1262138/


All Articles