4

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?

1
  • 1
    mydata$species <- factor(mydata$species, levels=c("Alpine Swift","Azure-winged Magpie"...). Commented May 14, 2014 at 17:28

3 Answers 3

10
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")

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @jlhoward What if you'd like a specific order of the y-axis variables which is not alphabetical? Let's say BACDEF... ?
3

Using fct_rev() from package forcats and following jlhoward's example:

library(ggplot2)
library(forcats)

df <- data.frame(x = rnorm(10), Species = LETTERS[1:10])

# original plot
ggplot(df) + 
  geom_point(aes(x = x, y = Species), size = 3, color = "red")

# solution
ggplot(df) + 
  geom_point(aes(x = x, y = fct_rev(Species)), size = 3, color = "red")

Comments

2

Try changing +scale_y_discrete("Species") to +scale_y_discrete("Species", trans = 'reverse')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.