6

I would like to change a base font for all geom_text elements.

library(ggplot2)

df <- data.frame(
  x = c(1, 2, 3), y = c(1, 2, 3), label = c("a", "b", "c")
)

ggplot(df, aes(x, y, label = label)) + geom_text()

base size

I tried setting theme text, but it doesn't seem to effect geom_text elements.

ggplot(df, aes(x, y, label = label)) + 
  geom_text() + 
  theme(text = element_text(size = 42))

size = 42

I've seen some older posts suggesting using base_size but it looks like it is no longer supported.

user10355350
  • 178
  • 2
  • 7
  • Possible duplicate of [Can't change fonts in ggplot/geom\_text](https://stackoverflow.com/questions/14733732/cant-change-fonts-in-ggplot-geom-text) – Vishesh Shrivastav Sep 16 '18 at 21:52
  • 1
    I don't think this is possible, for a good reason (but perhaps I will stand corrected): The difference being that (1) the font size of theme elements is fixed and can be changed globally through e.g. `theme` or `theme_set(theme_grey(base_size = 42))` (this is still supported), and (2) the `size` in `geom_text` is a `geom_*` aesthetic and can be dynamically mapped to variables. While you can fix `size` in `geom_text`, you can see how the mapping is different by setting `size = 42` inside `geom_text` giving a different (larger) font size than `size = 42` inside `element_text`. – Maurits Evers Sep 16 '18 at 22:16
  • 1
    For those who come later, this _is_ a hack, see https://stackoverflow.com/a/76222642/3358272 (override the `.pt` value stored in the `ggplot2` namespace). – r2evans May 10 '23 at 21:53

1 Answers1

-2

You can control the size by using it inside the geom_text() function.

library(ggplot2)


df <- data.frame(
  x = c(1, 2, 3), y = c(1, 2, 3), label = c("a", "b", "c")
)

# use 'size' in geom_text function
ggplot(df, aes(x, y, label = label)) + 
  geom_text(size = 10)

enter image description here

Suhas Hegde
  • 366
  • 1
  • 6
  • 13
  • 1
    Sometimes, you don't have access to geom_text. For instance, using package factoextra. Is there a solution then? – Josep Pueyo Feb 11 '22 at 08:56