简体   繁体   English

ggplot2中离散y轴的倒序

[英]Reverse order of discrete y axis in ggplot2

Situation & data情况与数据

I have a dataframe df of athlete positions in a race:我有一个比赛中运动员位置的数据框df

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给予

运动员职位

Question问题

How do I reverse the order of the y-axis so that 10 is at the bottom and 1 is at the top?如何反转 y 轴的顺序,使 10 在底部,1 在顶部?

There is a new solution, scale_*_discrete(limits=rev) , example:有一个的解决方案scale_*_discrete(limits=rev) ,例如:

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

在此处输入图像描述

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

add scale_y_discrete(limits = rev(levels(theFactor))) to your ggplot command.scale_y_discrete(limits = rev(levels(theFactor)))添加到您的 ggplot 命令。

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

在此处输入图像描述

For a discrete axis, using reorder() worked for me.对于离散轴,使用 reorder() 对我有用。 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.希望这可以帮助。

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:然后使用您的代码,您将获得:在此处输入图像描述

Another option using the forcats package.使用forcats包的另一种选择。

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")这具有将所有内容保留在ggplot调用中的优点,并且可以很好地与其他选项(例如coord_flipfacets_wrap(..., scales = "free")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM