0

I'm trying to find the font size of the x axis labels in ggplot2. I've used theme_classic.

I looked at the help documentation for theme_classic which says that size is 11. This is definitely not the font size because I manually changed the font size of the labels to 11 and the labels became bigger.

gamer220
  • 3
  • 1
  • 1
    You can still add a layer after `theme_classic()`: something like `+ theme(axis.text.x = element_text(size = 15))` – LMc Aug 29 '23 at 22:58
  • 1
    Here is [more detail](https://stackoverflow.com/questions/17311917/ggplot2-the-unit-of-size) about the font size in ggplot2. It might be different than what you are used to/expect. – LMc Aug 29 '23 at 23:00

1 Answers1

1

For taking the size of the x-axis text from the theme is pretty straightforward.

library(ggplot2)

my_theme <- theme_light(base_size = 10)

calc_element("axis.text.x.bottom", my_theme)$size
#> [1] 8

For extracting this from a plot, you'd need to build the plot first. If you want to reliably replicate the theme settings used during drawing the plot, you'd need to complete the theme using an internal function (which I generally don't recommend, but point out nonetheless).

my_plot <- ggplot(mtcars, aes(disp, mpg)) +
  geom_point() +
  theme(text = element_text(size = 20))

calc_element(
  "axis.text.x.bottom",
  ggplot2:::plot_theme(ggplot_build(my_plot)$plot)
)$size
#> [1] 16

Created on 2023-08-30 with reprex v2.0.2

teunbrand
  • 33,645
  • 4
  • 37
  • 63