Search code examples
rif-statementcase-whensubscript

Adding upper subscript on conditional ifelse in R


I'm struggling to format Name column in such a way that it adds an upper subscript when the Value column is larger than 10. Any suggestions? Here is a sample dataset.

 tibble(Name = LETTERS[1:10], 
        Value = sample(5:15,10))

The result should be like this: A^1 when Value is greater then 10


Solution

  • set.seed(123)
    d <- tibble(Name = LETTERS[1:10], 
           Value = sample(5:15,10)) %>%
      mutate(Name = if_else(Value>10, paste0(Name, "^1"), Name))
    
    
    library(ggplot2)
    ggplot(d, aes(Name, Value)) + geom_bar(stat = "identity") +
     scale_x_discrete("Axis label", labels = parse(text = d$Name)) +
      theme(axis.text.x = element_text(vjust=0))
    

    enter image description here