1

Sorry if this is very basic, but I am quite new to this. I am creating boxplots for some survey data in R using boxplot(). I notice it automatically arranges the x groups in alphabetical order, which doesn't suit my needs. In my data frame I changed the names to have an 'a' or 'b' at the beginning. This puts them in the order I want but then the label isn't neat. I would like to be able to override the x labels from the data frame with my own labels OR decide the order of the values along the x axis. Basically I just want 'Riparian' to come first, then 'Floodplain'. I have a number of other boxplots to do as well, some will have more than 2 values on the x axis.

This is the data and the code without trying to override the labels

data

Script

boxplot(tot_sp ~ hab, data = mydata, xlab= "Habitat Type", ylab = "Total # Species") 
title("Bird Search: Total Species")

boxplot enter image description here

Some other scripts I have tried, none have worked.

boxplot(tot_sp ~ hab, data = mydata, axis(1, at=seq(1, 2), labels = labels.name), 
        xlab= "Habitat Type", ylab = "Total # Species") 

boxplot(tot_sp ~ hab, data = mydata, 'labels',{'Riparian', 'Floodplain'}, at=NULL, 
        xlab= "Habitat Type", ylab = "Total # Species") 

boxplot(tot_sp ~ hab, data = mydata, xtix = {'Riparian', 'Floodplain'}, xtixloc = [1, 2], 
        xlab= "Habitat Type", ylab = "Total # Species") 

Thanks very much for your help!

neilfws
  • 32,751
  • 5
  • 50
  • 63
L.Mac
  • 13
  • 1
  • 1
  • 3

1 Answers1

3

Here's a stab in the dark:

mydata$hab <- factor(mydata$hab, levels=c("Riparian", "Floodplain")

boxplot(tot_sp ~ hab, data = mydata,
        xlab= "Habitat Type", ylab = "Total # Species") 

Example of reordering with iris data-set:

df <- iris
df$Species <- factor(df$Species, levels=c("virginica", "setosa", "versicolor"))
boxplot(Sepal.Length ~ Species, df)

enter image description here

Adam Quek
  • 6,973
  • 1
  • 17
  • 23