showtext icon indicating copy to clipboard operation
showtext copied to clipboard

Incompatibility with ggplotGrob()

Open nalsalam opened this issue 5 years ago • 2 comments

I'd like to use non-standard fonts in a ggplot but I also want to manipulate some widths so that several plots align vertically. Unfortunately after converting to a plot Grob all font information is lost. This may be related to issue #1.

R 3.5.1, ggplot2 3.0.0, showtext 0.5-1

library(ggplot2) data <- data.frame(x = c(rnorm(1000)))

before <- ggplot(data = data) + geom_histogram(aes(x), fill = "steelblue", color = "white", bins = 30) + labs(title = "Histogram of Normal Random Numbers", subtitle = "Histogram of Normal Random Numbers", y = "Frequency", x = "Normal Random Variable")

ggsave("before.pdf", plot = before)

(I have a need to modify widths of plots, so I create a plot Grob. Not showing that here.)

before2 <- ggplotGrob(before) ggsave("before2.pdf", plot = before2, width = 7, height = 7)

But I want to use non-standard fonts, so...

library(showtext) font_add_google("Schoolbell", "bell")

after <- before + theme(text = element_text(family = "bell")) + annotate("text", x = 2, y = 70, label = "N = 1000", size = 10, family = "bell")

as expected, the pdf has the non-standard font

showtext_auto() ggsave("after.pdf", plot = after, width = 7, height = 7) showtext_auto(F)

Oh, no! All text disappears

showtext_auto() after2 <- ggplotGrob(after) ggsave("after2.pdf", plot = after2, width = 7, height = 7)

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :

invalid font type

In addition: There were 50 or more warnings (use warnings() to see the first 50)

showtext_auto(F)

No errors or warnings but non-standard font is replaced with a serif font

showtext_auto() ggsave("after2.svg", plot = after2, width = 7, height = 7)

nalsalam avatar Jul 11 '18 14:07 nalsalam

You'll probably need extrafont package for ggplotGrob or tableGrob objects (for example outputs from cowplot's plot_grid() or add_sub() functions)

# install.packages('extrafont', dependencies = TRUE)
library(extrafont)

### Run this one only ONCE to import fonts to R
font_import()

### Load fonts
extrafont::fonttable()
# Can be "pdf" (default), "postscript", or "win"
loadfonts(device = "win")

tungttnguyen avatar Aug 31 '18 17:08 tungttnguyen

I encountered a similar issue (text disappearing while the rest of the plot shows) in the shiny app. Would love to see if there's a solution to this issue to get inspired (I used ggplotGrob for other graphs earlier today, which may or may not be the cause of my shiny app plot issue).

For the specific issue of using ggplotGrob with showtext as mentioned by @nalsalam, I updated R to 3.5.2 and can still replicate the issue. The solution suggested by @tungmilan did not work, unfortunately.

===== Update 1/11/19: Just to update that my issue of text disappearing of the plots in the shiny app got resolved, in case anyone would found this tread. The issue is that I changed the monitor settings from “True Color (32bit)” to “High Color (16 bit)” in the control panel. Once it is switched back to “True Color (32bit)”, the texts are showing up again in the shiny app.

yuqiliao avatar Jan 09 '19 23:01 yuqiliao