Search code examples
rboxplotordinal

R: Levels/Labels for ordinal variables and the boxplot function


I used to have my ordinal data as plain numbers in the data frame (i.e. 1-3). This worked out fine, but I had to remember what the numbers stood for (in my case: 1=Low, 2=Medium, 3=High).

To make things easier to me I am trying to get R to display names/labels instead of 1-3 in a generic fashion, i.e. regardless of whether I am using tables, boxplots, etc. However, I can't get this to work.

I tried

data$var <- ordered(data$var, 
    levels = c(1, 2, 3), 
    labels = c("Low", "Medium", "High")) 

After I do this, a

boxplot(data$var)

does not work anymore. Why not?


Solution

  • The command didn't work because boxplot is expecting the first argument (the x argument), to be numeric.

    If you're only looking for a simple solution, you could simply plot the data as integers (as your factors are ordered, therefore they will be in the right integer order) suppress the original axes and add a new one with the right axis labels.

    boxplot(as.integer(data$var), yaxt = "n")
    axis(2, at = c(1, 2, 3), labels = c("Low", "Medium", "High"))