99

Situation & data

I have a dataframe df of athlete positions in a race:

df <- structure(list(athlete = c("A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "A", "B", "C", 
"D", "E", "F", "G", "H", "I", "J", "A", "B", "C", "D", "E", "F", 
"G", "H", "I", "J", "A", "B", "C", "D", "E", "F", "G", "H", "I", 
"J"), distanceRemaining = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L), .Label = c("1400m", "1200m", "600m", 
"400m", "200m", "FINISH"), class = "factor"), position = c(10, 
6, 7, 8, 2, 1, 3, 5, 9, 4, 9, 8, 7, 6, 4, 3, 1, 5, 10, 2, 8, 
7, 9, 5, 6, 2, 3, 1, 10, 4, 9, 8, 6, 5, 7, 3, 2, 4, 10, 1, 4, 
5, 1, 6, 8, 3, 2, 7, 10, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), row.names = c(NA, 
-60L), .Names = c("athlete", "distanceRemaining", "position"), class = "data.frame")

I'm plotting the data with

library(ggplot2)
g <- ggplot(df, aes(x=distanceRemaining, y =position, colour=athlete, group = athlete))
g <- g + geom_point()
g <- g + geom_line(size=1.15)
g <- g + scale_y_discrete()
g

To give

athletePositions

Question

How do I reverse the order of the y-axis so that 10 is at the bottom and 1 is at the top?

tospig
  • 7,762
  • 14
  • 40
  • 79

6 Answers6

143

There is a new solution, scale_*_discrete(limits=rev), example:

tibble(x=1:26,y=letters) %>% 
  ggplot(aes(x,y)) +
    geom_point() +
    scale_y_discrete(limits=rev)

enter image description here

Frederik Ziebell
  • 1,563
  • 1
  • 10
  • 9
54

as per https://gist.github.com/jennybc/6f3fa527b915b920fdd5:

add scale_y_discrete(limits = rev(levels(theFactor))) to your ggplot command.

bdemarest
  • 14,397
  • 3
  • 53
  • 56
Yossi Farjoun
  • 2,180
  • 3
  • 17
  • 25
41

Try the following:

g <- ggplot(df, aes(x=distanceRemaining, y =position, colour=athlete, group = athlete))
g <- g + geom_point()
g <- g + geom_line(size=1.15)
g <- g +  scale_y_continuous(trans = "reverse", breaks = unique(df$position))
g

enter image description here

DatamineR
  • 10,428
  • 3
  • 25
  • 45
  • 3
    Can you use `scale_y_reverse()` with `scale_y_discrete()`, or another method to keep the whole numbers on the y axis? – tospig Feb 08 '15 at 09:03
  • 22
    This doesn't work for me in at least with ggplot2 version 2.1. `scale_y_continuous` will throw an error if you feed it a factor. The version to use for a factor would be `scale_y_discrete(limits = rev(levels(df$distanceRemaining))` . – Curt F. Jun 29 '16 at 00:09
  • 4
    For future people that are stuck, if you had created a group_df with dplyr at some point, you may need to first ungroup your tibble to get the factors in the right order. – Jordan Apr 02 '17 at 18:54
  • 1
    probably the most useful answer is from @Frederik Ziebell – fc9.30 Dec 09 '20 at 15:14
  • this to me is the best answer. for larger codebases with lots of conditions, you don't have to have to repeat code by having `scale_y_discrete()` for one scenario, and `scale_y_reverse()` for another. just having an argument you can turn on and off with 'reverse' and 'identity' is extremely useful – Matt Jan 19 '23 at 00:21
40

For a discrete axis, using reorder() worked for me. In context of the above problem, it would look something like this:

ggplot(df, aes(x = distanceRemaining, y = reorder(position, desc(position))))

Hope this helps.

David Bloom
  • 517
  • 4
  • 3
  • I find this to be the best solution, as it doesn't require to point to anything other than what has been already passed to ggplot. Thank you! – Stefano Jun 26 '20 at 08:32
  • use `reorder(position -position` should one choose not to use `dplyr` – Sweepy Dodo Oct 27 '21 at 08:59
26

You just need to turn the position variable into a factor and then reverse its levels:

require(dplyr)
df <- df %>% mutate(position = factor(position), 
                    position = factor(position, levels = rev(levels(position)))

And then with your code you'd get: enter image description here

Peter Diakumis
  • 3,942
  • 2
  • 28
  • 26
  • Thanks, but I'm going with the answer that keeps it within the `ggplot2` call. – tospig Feb 08 '15 at 09:42
  • 5
    The `trans = "reverse"` approach doesn't work for truly discrete (e.g. character) scales. `scale_y_reverse()` would also have worked since your y-axis can be coerced to a continuous scale... – Paul 'Joey' McMurdie Jul 01 '16 at 00:58
  • you can keep it in the ggplot2 call by `ggplot(df, aes(x=distanceRemaining, y=factor(position, levels=c(1:10)), colour=athlete, group = athlete))` – Brian D Oct 12 '17 at 18:04
  • Thanks, this solved an annoying problem with `coord_flip()` – userABC123 Nov 30 '17 at 18:45
10

Another option using the forcats package.

ggplot(df, aes(x = distanceRemaining, y = forcats::fct_rev(factor(position))))

This has the advantage of keeping everything in the ggplot call, and playing nicely with other options such as coord_flip and facets_wrap(..., scales = "free")

JWilliman
  • 3,558
  • 32
  • 36