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!