-1

Is it possible to achieve the same result as in the X-axis below using just standard code from the ggplot2 package (or any of the tidyverse packages)? I would like to have an axis label that consists of 2 different font sizes (e.g., 12pt and 8pt) without having to resort to loading additional packages (such as ggtext below).

library(tidyverse)
library(ggtext) # For element_markdown()

mtcars |> 
  ggplot() +
  aes(x = wt, y = mpg) +
  geom_point() +
  labs(x = "Miles/(US) gallon<br><span style = 'font-size:8pt'>[Min = 10.4, Max = 33.9]</span>") +
  theme(axis.title.x = element_markdown())

captain
  • 543
  • 1
  • 3
  • 20
  • 2
    Why not just use `ggtext`? – Captain Hat Dec 14 '22 at 10:47
  • 1
    Because I find it tedious to always having to add an additional `+ theme(...)` line, and I want to reduce the number of packages to load. This especially becomes an issue when I am producing *a lot* of figures for a project. – captain Dec 14 '22 at 10:50
  • I found a way to do it buried in one of the answers to the question that you link to above. It's not the accepted answer, though, so I am posting this here again so people can find it quickly. Feel free to mark either question as a duplicate of the other. – captain Dec 14 '22 at 11:29
  • Rather than posting a question and answer again, just upvote the answer at the other post that you think was better than the accepted one. There are only 3 answers there, so it shouldn't require that much digging – camille Dec 14 '22 at 17:01

1 Answers1

1

This is one neat solution using base R functions.

library(tidyverse)

mtcars |> 
  ggplot() +
  aes(x = wt, y = mpg) +
  geom_point() +
  labs(x = expression(atop("Miles/(US) gallon", atop("[Min = 10.4, Max = 33.9]"))))

captain
  • 543
  • 1
  • 3
  • 20