user3637399
user3637399

Reputation: 41

Reorder label y axis in ggplot

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?

Upvotes: 4

Views: 15133

Answers (3)

canovasjm
canovasjm

Reputation: 523

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

Upvotes: 3

Jordan
Jordan

Reputation: 375

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

Upvotes: 2

jlhoward
jlhoward

Reputation: 59425

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

Upvotes: 10

Related Questions