Search code examples
rggplot2labelaxes

How to add a text subscript to a special character in ggplot2 factor label?


I need to first relabel the factors such that there is alpha symbol followed by text-subscripts. I followed a few examples from a few places and tried the following.

df <- data.frame(alpha = c("aii","aij","ajj","aji"), count = c(1,2,3,4))

ggplot(aes(x = alpha, y = count), data = df) + geom_points()+
    scale_x_discrete(labels=c("aii" = bquote(\u03b1 [ii]), "aij" = bquote(\u03b1 [ij]),
                              "ajj" = bquote(\u03b1 [jj]),"aji" = bquote(\u03b1 [ji]))

where \u03b1 is the unicode for alpha

I get Error: unexpected input in: "aii" = bquote(\u03b1 [ii])

Any suggestions on how to proceeed.


Solution

  • bquote() needs quotes around the unicode, and the call to geom_point() is misspelled.

    library(ggplot2)
    
    df <- data.frame(alpha = c("aii","aij","ajj","aji"), count = c(1,2,3,4))
    
    ggplot(aes(x = alpha, y = count), data = df) + 
      geom_point() +
      scale_x_discrete(labels=c("aii" = bquote("\u03b1" [ii]), "aij" = bquote("\u03b1" [ij]), 
                                "ajj" = bquote("\u03b1" [jj]),"aji" = bquote("\u03b1" [ji])))
    

    enter image description here