1
votes

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:

  • Name of the variables (e.g. niceness)
  • A second line with a tag which describe each extreme value of the respective axis (e.g. jerk - nice)

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
         ))
1
Note from the package documentation: "the gridtext package that provides the rendering support implements only an extremely limited subset of Markdown/HTML/CSS". It doesn't look like "word-spacing" is supported. You can file a github issue if you like.MrFlick
I can see your effort to make a reproducible example - but your missing a call to magrittr (for the pipe) (or just tidyverse), or it's unclear where TeX() is fromtjebo
to avoid this - I'd advise using the reprex packagetjebo
@tjebo you are absolutelty right, the missing dependency was latex2exp::TeX(); latelty I will modify the question to use reprex as you suggestCristóbal Alcázar

1 Answers

3
votes

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