1

I am making a plot and using a custom from Google using the {sysfonts} package. In order for this to run, I also have to run showtext_auto from the {showtext} package. Whenever I use the new font with geom_richtext() function from the {ggtext} package, the spacing gets distorted. Additionally, when I add any plots with these fonts to a plot_grid from the {cowplot} package, the alignment gets thrown off too.

In my specific example, I am using the geom_richtext() function to bold parts of a label.

Here is some sample data:

df <- data.frame(x1=c(0,1,2), y1=c(0,0,0), x2=c(1,2,3), y2=c(3,3,3), text=c("**Low** (0-100)","Medium (100-200)","**High** (200+)"))

The "*" around the labels indicate bolding in the geom_richtext() function

and here is a plot I am trying to make

library(ggplot2)
library(sysfonts)
library(showtext)
library(ggtext)

font_add_google("Noto Sans JP")
showtext_auto()
x <- ggplot() +
  geom_rect(data=df, mapping = aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2), fill=c("Red", "Orange", "Yellow")) +
  geom_richtext(data=df, aes(x=x1+(x2-x1)/2, y=y1+(y2-y1)/2, label=text), size=15, angle=90, family="Noto Sans JP",
                fill= NA, label.color=NA) +
  theme_void() 

plot(x)
ggsave("~/Desktop/IndividualGraph.png)

plot_grid(x, x)
ggsave("~/Desktop/DuoGraph.png)

When run this, there is a strange gap between the words and the the numbers in parenthesis, this gap also does not disappear if I remove the spaces from the labels. When I put them together in a plot_grid (which something I need to do), and save the grid to my laptop, the alignment is also thrown off.

I have had other issues with alignment and spacing too when not using geom_richtext() so I think the issue is stemming from the font_add_google() and plot_grid(), I am just unsure of how to fix this.

If I remove the font, then they save normally, however, I would really like to use these fonts. Are the packages just incompatible or is there some way to fix this issues of alignment and spacing when saving?

Thanks!

OKLM
  • 85
  • 7
  • One option to fix your second issue would be to have a look at `patchwork`. Concerning your first issue I'm unfortunately not able to reproduce your issue on my Mac. – stefan Aug 26 '21 at 18:28

1 Answers1

0

This is due to the interaction between ggtext (the package providing the markdown/richtext) and the showtext package. Same goes for the cowplot package.

Claus Wilke, the developer of both cowplot and ggtext, mentioned this is a related GitHub issue:

Now that I'm reading a bit more about showtext, I see many ways how this could go wrong in conjunction with cowplot and ggtext. You have three packages that all do some slightly shady things to get results you otherwise couldn't get. https://github.com/wilkelab/cowplot/issues/167#issuecomment-683461043

I'm not sure how to fix this outside of RStudio (for e.g. GitHub Actions), but I solved it in RStudio (v2021.09.1) by changing the Graphics Device to AGG:

enter image description here

Running your example:

enter image description here

MS Berends
  • 4,489
  • 1
  • 40
  • 53