Reorder label y axis in ggplot

ggplot2, r

Solution

library(ggplot2)
df <- data.frame(x=rnorm(10),Species=LETTERS[1:10])
ggplot(df)+geom_point(aes(x=x,y=Species),size=3,color="red")
df$Species <- factor(df$Species,levels=rev(unique(df$Species)))
ggplot(df)+geom_point(aes(x=x,y=Species),size=3,color="red")

If you want to put y in some other order, say order of decreasing x, do this:

df$Species <- factor(df$Species, levels=df[order(df$x,decreasing=T),]$Species)
ggplot(df)+geom_point(aes(x=x,y=Species),size=3,color="red")

Problem

I am trying to have labels of the y axis from a ggplot between a categorical (species in Y) and a continuous variable (in X) presented in alphabetic order. But I am getting the Y presented with the last species in alphabetic order on the top of my Y axis and the first species in alphabetic order on the bottom. Since I am new I cannot show images, but it looks like a list of species on the y axis and for each species is represented a point with its standard error bars to the corresponding x value (mean). And the species are presented with Wood Duck on the top and Alpine Swift on the bottom (the middle being ordered in alphabetic order). I would like to have the opposite (species Alpine Swift on the top and on the bottom the species Wood Duck). the command I used to plot the graph is the following: ``` # getting data for the error bars limits<-aes(xmax=mydata$Xvalues+mydata$Xvalues_SD, xmin=(mydata$Xvalues-mydata$Xvalues_SD)) # plot graph graph<-ggplot(data=mydata,aes(x = Xvalues, y = species)) +scale_y_discrete("Species") +scale_x_continuous(" ")+geom_point()+theme_bw()+geom_errorbarh(limits) ``` I have tried to order my data set before to upload the data and run the graph. I have also tried to reorder the species factor using the following command: ``` mydata$species <- ordered(mydata$species, levels=c("Alpine Swift","Azure-winged Magpie","Barn Swallow","Black-browed Albatross","Blue Tit1","Blue Tit2","Blue-footed Booby","Collared Flycatcher","Common Barn Owl","Common Buzzard","Eurasian Sparrowhawk","European Oystercatcher","Florida Scrub-Jay","Goshawk","Great Tit","Green Woodhoopoe","Grey-headed Albatross","House Sparrow","Indigo Bunting","Lesser Snow Goose","Long-tailed Tit","Meadow Pipit","Merlin","Mute Swan","Osprey","Pied Flycatcher","Pinyon Jay","Sheychelles Warbler","Short-tailed Shearwater","Siberian Jay","Tawny Owl","Ural Owl","Wandering Albatross","Western Gull1","Western Gull2","Wood Duck")) ``` But I am getting the same graph. How should I do to change the order of my Y axis?

Original source