Home > Back-end >  How to type subscript/superscript in the category labels in bar plot?
How to type subscript/superscript in the category labels in bar plot?

Time:01-16

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., Fig.1

CodePudding user response:

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())

  •  Tags:  
  • Related