1

I'm trying to create a ggplot with markdown text in the plot title, subtitle, the caption and in the legend text. The legend text is dependent on a variable, which there is thus and ifelse-statement in the making of the legend text. In one of the cases, I want to have a part of the text in italics and I'm therefore trying to use the element_markdown-function from the ggtext package, and just wrap that part of the text in '*'. However, I'm facing some issues with rendering it properly with ggplot2. When using the element_markdown-function, the text gets very tight and overlapping, and looks terrible. I've tried to adjust the font size, face and family, as well as the margins around the areas in question, but no luck. I've also tried to use alternative functions, such as expression with paste or glue, but the ifelse-statement makes this difficult, and I've even tried using UNICODE symbols as input instead. I have not managed to do what I'm trying to do no matter which approach I've attempted, and I really think this should be doable with the element_markdown-function, so I'm hoping for some help here.


I'm running R version 4.2.1 (2022-06-23) on macOS Monterey 12.5.1, via RStudio version 2022.7.1.554 (Spotted Wakerobin). I'm using ggtext package version 0.1.1.



Minimal example and output I'm getting:


library(tidyverse)
library(glue)
library(ggtext)

random_variable <- TRUE

iris %>% 
    ggplot(aes(x = Species, y = Sepal.Width)) + 
    geom_boxplot(show.legend = F, col = 'cadetblue4') +
    stat_summary(fun = mean, geom = 'point', shape = 18, size = 3, aes(col = 'mean')) +
    geom_hline(aes(yintercept = 2, col = '1st'), size = .5, lty = 2) +
    geom_hline(aes(yintercept = 3, col = '2nd'), size = .5, lty = 2) +
    geom_hline(aes(yintercept = 4, col = '3rd'), size = .5, lty = 2) +
    scale_colour_manual(name = 'Title without markdown', 
                        values = c('1st' = 'slateblue2', '2nd' = 'coral4', '3rd' = 'red2', 'mean' = 'black'),
                        labels = c('Sub<sub>1</sub>', 'Sup<sup>2</sup>', 'Both<sub>3</sub><sup>10</sup>', 
                                   paste('Some', ifelse(random_variable, yes = '*markdown* text here', no = 'not markdown text here'), 'and then some more text here.')),
                        guide = guide_legend(override.aes = list(
                            linetype = c(rep('dashed', 3), 'blank'),
                            shape = c(rep(NA, 3), 18)))) + 
    labs(title = paste0("This is a test of element_", ifelse(T, yes = '*markdown*', no = 'text'), " with the Iris dataset!"),
         subtitle = "Subtitle with **boldface text** and *italicised text* -- Not working at all…",
         caption = "THIS SENTENCE DEFINITELY HAS WHITESPACES!^{23}") +
    theme(
          # TITLES AND CAPTION
          plot.title = element_markdown(family="Times", face = "bold", size=15),
          plot.subtitle = element_markdown(family="Times", size=10),
          plot.caption = element_markdown(family = "Times", face = "bold", size = 11),
          
          # LEGEND SETTINGS
          legend.position = 'bottom',
          legend.key.size = unit(1.75, 'line'),
          legend.spacing.x = unit(.1, 'cm'),
          legend.title = element_markdown(size = 12, face = 'bold', family = 'Times', 
                                      margin = margin(r = .25, unit = 'cm')),
          legend.text = element_markdown(size = 13, face = 'bold', family = 'Times', 
                                     margin = margin(r = .5, unit = 'cm')),
          legend.key =  element_blank())


Minimal example output

OMMJREN
  • 57
  • 7
  • 1
    You could try with installing the dev version of `gridtext`. See the related issues https://github.com/wilkelab/ggtext/issues/92 and https://github.com/wilkelab/gridtext/issues/24 – stefan Sep 06 '22 at 10:35
  • Aah, look at that! I had actually already installed the dev version of the package with `remotes::install_github("wilkelab/gridtext")`, but not loaded the `gridtext` package. After loading it with `library(gridtext)` it works! Thanks a lot! – OMMJREN Sep 06 '22 at 11:33

1 Answers1

0

Was resolved by installing the dev version of gridtext with remotes::install_github("wilkelab/gridtext") and then loading the package with library(gridtext).

Thanks to @stefan for the tip in the comments of the original post!

OMMJREN
  • 57
  • 7