Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatibility with ggplotGrob() #22

Open
nalsalam opened this issue Jul 11, 2018 · 2 comments
Open

Incompatibility with ggplotGrob() #22

nalsalam opened this issue Jul 11, 2018 · 2 comments

Comments

@nalsalam
Copy link

nalsalam commented Jul 11, 2018

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)

@tungttnguyen
Copy link

tungttnguyen commented Aug 31, 2018

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

@yuqiliao
Copy link

yuqiliao commented Jan 9, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants