rggplot2ggtext

Add white spaces between two words in a label using ggtext


I using the ggtext package for formatting the axis labels of a plot made by ggplot2, specifically the function element_textbox_simple(), and I have some issues with the html format to introduce white spaces or a blank space between words.

enter image description here

In the above plot, each axis label have two levels:

For achieve this "multi-level label" I used ggtext to format the label with the following html expression:

<br><span style = 'font-size:8pt'>Niceness</br><br><span style='word-spacing:20pt;font-size:10pt;color:red'>JERK NICE</span></br>"

My problem is that the attribute word-spacing don't work and I need to space the "second line with tags" of each axis to the extremes of the axis. For example, in the plot I want to replace the text between JERK and NICE with many empty spaces. I tried to use nbsp; which supposed that encode a white space but without success, it only separate the two tags by one default space. Any idea?

You can reproduce the plot with:

library(ggplot2)
library(ggtext)
library(latex2exp)

# Multivariate normal dist parameters:
mu <- c(50, 50)
Sigma <- matrix(c(200, 4, 8, 200), 2)

# Generate 10.000 data points
set.seed(323)
df <- as.data.frame(MASS::mvrnorm(5e3, mu, Sigma))

# Compute correlation between V1 and V2
cor0 <- round(cor(df$V1, df$V2), 2)

# Initial plot
p0 <- df %>% 
   ggplot() +
   geom_point(aes(V1, V2), alpha = .3, size = .2) +
   scale_x_continuous(breaks = seq(0, 100, 20), n.breaks = seq(0, 100, 5)) +
   scale_y_continuous(breaks = seq(0, 100, 20)) +
   coord_cartesian(xlim = c(0, 100), ylim = c(0, 100)) +
   labs(x = "<br><span style = 'font-size:8pt'>Niceness</br><br><span style='word-spacing:5pt;font-size:10pt;color:red'>JERK  (replace this text by a blank separation between these two extrem label descriptions)  NICE</span></br>",
   y = "<br><span style = 'font-size:8pt'>Attractiveness</br><br><span style = 'font-size:10pt;color:red'>NOT  (replace this text by a blank separation between these two extrem label descriptions)  HOT</span></br>",
   title = TeX(paste("$\\rho$ =", as.character(cor0)))) +
   theme_bw(base_size = 8) +
   theme(plot.title = element_text(hjust = 0.5),
         panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         axis.title.x = element_textbox_simple(
                          margin = margin(0, 100, 15, 100),
                          halign = 0.5
                        ),
         axis.title.y = element_textbox_simple(
                          margin = margin(0, 0, 0, 0),
                          orientation = "left-rotated",
                          halign = 0.5
         ))

Solution

  • Perhaps you can just annotate the text you want as shown below. Please note the clip="OFF" in coord_cartesian

    Also, you can repeat the same for y-axis.

    # Initial plot
    p0 <- df %>% 
      ggplot() +
      geom_point(aes(V1, V2), alpha = .3, size = .2) +
      scale_x_continuous(breaks = seq(0, 100, 20), n.breaks = seq(0, 100, 5)) +
      scale_y_continuous(breaks = seq(0, 100, 20)) +
      coord_cartesian(xlim = c(0, 100), ylim = c(0, 100), expand = TRUE, clip = "off") +
      labs(# x = "<br><span style = 'font-size:8pt'>Niceness</br><br><span style='word-spacing:5pt;font-size:10pt;color:red'>JERK  (replace this text by a blank separation between these two extrem label descriptions)  NICE</span></br>",
           x = "Niceness",
           y = "<br><span style = 'font-size:8pt'>Attractiveness</br><br><span style = 'font-size:10pt;color:red'>NOT  (replace this text by a blank separation between these two extrem label descriptions)  HOT</span></br>",
           title = "My Title" #TeX(paste("$\\rho$ =", as.character(cor0)))
           ) +
      annotate(geom = "text", x = c(0,100), y = -15, 
               label = c("JERK", "NICE" ), size = 5, color="red") +
      theme_bw(base_size = 8) +
      theme(plot.title = element_text(hjust = 0.5),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            axis.title.x = element_textbox_simple(
              margin = margin(0, 100, 15, 100),
              halign = 0.5
            ),
            axis.title.y = element_textbox_simple(
              margin = margin(0, 0, 0, 0),
              orientation = "left-rotated",
              halign = 0.5
            ))
    p0
    

    output