Home > front end >  r add math symbol to x axis text
r add math symbol to x axis text

Time:05-13

I am trying to add a math symbol (sum) to some texts in x-axis.

For example, if this is my plot.

data(iris)
p1 <- ggplot(iris, aes(x=Species, y=Sepal.Length))   
  geom_boxplot()
p1

What I am trying to accomplish is this. Not sure how to do this, any suggestions appreciated. Thanks in advance.

enter image description here

CodePudding user response:

This is almost certainly a duplicate, but I couldn't find the answer when I searched, so here is a potential solution:

library(tidyverse)

iris_correctly_labelled <- iris %>%
  mutate(Species = case_when(Species == "setosa" ~ paste0("\u2211", "setosa"),
                             Species == "versicolor" ~ "versicolor",
                             Species == "virginica" ~ paste0("\u2211", "virginica")))

p1 <- ggplot(iris_correctly_labelled,
             aes(x=fct_reorder(Species, Sepal.Length), y=Sepal.Length))   
  geom_boxplot()  
  theme(axis.title.x = element_blank(),
        text = element_text(size = 16))
p1

Created on 2022-05-13 by the reprex package (v2.0.1)

Does that solve your problem?

  • Related