0

I would like to reduce a string size (i.e., y and x) in one character for the axis created by ``ggplot` as picture below

Anyone has suggestions for me, please?

enter image description here

Anh
  • 735
  • 2
  • 11
  • Potential duplicate of: https://stackoverflow.com/questions/53379014/barplot-label-subscripts-without-using-expression/53380299#53380299 https://stackoverflow.com/questions/17334759/subscript-letters-in-ggplot-axis-label https://stackoverflow.com/questions/69906143/including-subscript-greek-letter-symbols-to-ggplot-x-axis-text – jared_mamrot Nov 23 '21 at 23:07

1 Answers1

1

One potential solution is to use expression(), e.g.

library(tidyverse)
library(palmerpenguins)

penguins %>%
  na.omit() %>%
  ggplot(., aes(x = bill_length_mm, y = body_mass_g)) +
  geom_point() +
  theme_classic(base_size = 16) +
  ylab(expression(P[y])) +
  xlab(expression(P[x])) +
  theme(axis.title = element_text(hjust = 1),
        axis.line = element_line(arrow = arrow(type='closed', length = unit(12,'pt'))))

Created on 2021-11-24 by the reprex package (v2.0.1)

Another potential solution is to use unicode symbols (e.g. from https://unicode-table.com/en/sets/superscript-and-subscript-letters/):

penguins %>%
  na.omit() %>%
  ggplot(., aes(x = bill_length_mm, y = body_mass_g)) +
  geom_point() +
  theme_classic(base_size = 16) +
  xlab(paste("P", "\u1D6A", sep = "")) +
  ylab(paste("P", "\u1D67", sep = "")) +
  theme(axis.title = element_text(hjust = 1),
        axis.line = element_line(arrow = arrow(type='closed', length = unit(12,'pt'))))

Created on 2021-11-24 by the reprex package (v2.0.1)

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46