0

I am trying to create a plot in R, its very simple however this has been bugging me for days and I can not get it to work. When I make the plot R orders the y axis by alphabetical order, how do you specify the order you want. Using the example below:

x = c("High ind.", "High sp.",  "Mid ind.", "Mid sp.", "Low ind.", "Low sp.")
y = c(4.6, 2.3, 5.5, 2.2, 12.6, 3)
sd = c(3.2, 1.2, 4.4, 1.5, 5.9, 1.5)

qplot(x,y, xlab="Water level", 
      ylab="mean number of ind. and sp. with standard deviations") + 
   geom_errorbar(aes(x=x, ymin=y-sd, ymax=y+sd, width=0.2), color="blue")

I would like the order on the y axis like this: High, Mid, Low. However R automatically puts it in alphabetical order. I have tried many ways to fix this however they are all aimed at large more complex datasets, and do not work with such a simple set of data. There must be a simple way to fix this..

joran
  • 169,992
  • 32
  • 429
  • 468
  • All of the following are duplicates: [this](http://stackoverflow.com/q/12774210/324364), [this](http://stackoverflow.com/q/3253641/324364), [this](http://stackoverflow.com/q/3744178/324364) and [this](http://stackoverflow.com/q/8713462/324364). They all reference the same technique: order is controlled by using a factor and specifying the order of the levels. – joran May 29 '14 at 20:30
  • Hi joran, thanks I have looked through those and tried very hard to use the techniques they use however it never seems to work for me, I really have been trying for several days now... Each time I try to manually set the levels it just produces the same graph in alphabetical order or it comes up with error messages.. Thats why I wanted to check that it was not something I was specifically doing wrong? I have tried the code in the answer below however this still does not work... – user3689043 May 30 '14 at 08:43
  • The answer below was actually wrong so I fixed it. It now uses the exact same technique described in detail in each of the questions I mentioned and produces the exact output you say you wanted. – joran May 30 '14 at 14:10

1 Answers1

0

Use a factor and specify the level order manually:

lv <- c("High ind.", "High sp.",  "Mid ind.", "Mid sp.", "Low ind.", "Low sp.")
x <- factor(lv,levels = lv)

y = c(4.6, 2.3, 5.5, 2.2, 12.6, 3)
sd = c(3.2, 1.2, 4.4, 1.5, 5.9, 1.5)

dat<-data.frame(x, y, sd)

qplot(x,y, data=dat, xlab="Water level", ylab="mean number of ind. and sp. with standard 
deviations")+geom_errorbar(aes(x=x, ymin=y-sd, ymax=y+sd, width=0.2), color="blue")
joran
  • 169,992
  • 32
  • 429
  • 468
ZRoss
  • 1,437
  • 1
  • 15
  • 32
  • You don't need an ordered factor; you only need the levels specified in the desired order. – joran May 29 '14 at 20:26
  • Thanks, I have tried to do this, and have also tried the above however this does not seem to work which is why I decided to ask on here... Any other options / code I could try? – user3689043 May 30 '14 at 06:11
  • @ZRoss Please be aware that ordered factors are _not_ necessary, and in addition your original version was actually wrong; even if you specify `ordered = TRUE`, the default in `factor` is to set the levels using the lexicographically _sorted_ unique values. So if you don't explicitly pass something to `levels`, you will get them in alphabetical order again. – joran May 30 '14 at 14:12