Wednesday, January 07, 2015

Changing the order of x-axis factor labels using levels

I had some problems with my factors showing up in alphabetical order instead of the order I needed.

I googled this problem and found that I should be using levels.  The linked post is more about different ways this works, but I found it helpful in understanding the principles.

So, here's a set of invented data with a factor category that we'll examine.

ordinals<-c("first", "second", "fourth", "third", "fourth", "third", "second", "first")

plot(x=data$ordinals, y=data$numerals, #your x and y data
     xlab='ordinals', ylab='numerals') #always label your axes

#This plot orders the factors alphabetically.
#However, it makes no sense logically to have fourth come right after first.
#I've also used this when I want to order sites
#by geography instead of alphabetically.

#Let's look at the data structure.
#You can see that there are four levels of the factor data.frame$ordinals.
#'data.frame':  8 obs. of  2 variables:
#$ ordinals: Factor w/ 4 levels "first","fourth",..: 1 3 2 4 2 4 3 1
#$ numerals: num  1 2 4 3 4 3 2 1
#data$ordinals is ordered alphabetically (you can see fourth comes after first).

#To remedy this oddity, make a list ordering your levels as desired.
levels.we.want<-c("first", "second", "third", "fourth")

#Create a new column from the ordinals column,
#using the new level ordering as levels.
data$ordinals.ordered<-factor(data$ordinals, levels=levels.we.want)


#The results now show the new column has correctly ordered levels.
#'data.frame':  8 obs. of  3 variables:
#$ ordinals        : Factor w/ 4 levels "first","fourth",..: 1 3 2 4 2 4 3 1
#$ numerals        : num  1 2 4 3 4 3 2 1
#$ ordinals.ordered: Factor w/ 4 levels "first","second",..: 1 2 4 3 4 3 2 1

plot(x=data$ordinals.ordered, y=data$numerals, #your x and y data
     xlab='ordinals in order', ylab='numerals')#axis labels

#All better!

No comments:

Post a Comment

Comments and suggestions welcome.