Reputation: 613
I'd like to make subscript or superscript in the category labels (Fig.1) by ggplot2 in R. I found there are solutions for the axis text (e.g., this), but not for the category labels.
For example, if the bar chart has 3 categories (K_a, K_b, K_c), then there will be 3 places to change the form (not one place which is the axis text).
Upvotes: 1
Views: 586
Reputation: 125268
This could be achieved by passing scales::parse_format
to the labels argument of the scale. Using ?plotmath
notation you have to recode your categories as e.g. K[b]
to get a subscript "b":
df <- data.frame(
x = paste0("K[", letters[1:3], "]"),
y = 1:3
)
library(ggplot2)
ggplot(df, aes(x, y)) +
geom_col() +
scale_x_discrete(labels = scales::parse_format())
Upvotes: 2