r - ggplot2中离散y轴的逆序 [英] r - reverse order of discrete y axis in ggplot2

查看:837
本文介绍了r - ggplot2中离散y轴的逆序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况&数据



我在比赛中有一个数据框 df 的运动员位置(我已经融化它用于 ggplot2 ):

,H,I,J,A,B,C,D,E,F,G,H,I, J,
A,B,C,D,E,F,G,H,I,J ,B,C,
,D,E,F,G,H,I,J,A,B, CDEF
GHIJABCD ,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,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),标签= 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。框架)

我正在绘制数据

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

要给



问题

如何颠倒Y轴的顺序,使10位于底部,1位于顶部?

解决方案

尝试以下操作:

  g < -  ggplot(df,aes(x =距离剩余,y =位置,颜色=运动员,组=运动员))
g < - g + geom_point()
g < - g + geom_line(size = 1.15)
g < g + scale_y_continuous(trans =reverse,breaks = unique(df $ position))
g


Situation & data

I have a dataframe df of athlete positions in a race (I've already melted it for use with ggplot2):

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?

解决方案

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

这篇关于r - ggplot2中离散y轴的逆序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆