ggplot2,订购y轴 [英] ggplot2, Ordering y axis

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

问题描述

我有以下名为 df 的data.frame.我的问题与y轴上的顺序有关.我希望y轴上的名称根据变量 depth 进行排序.

I have the below data.frame called df. My problem has to do with the order on the y-axis. I want that the names on the y-axis are ordered according to the variable depth.

如果我这样做:

ggplot(df,aes(x=factor(name),y=depth)) + geom_bar(stat='identity') + coord_flip() + labs(y='depth',x='species')

我得到了 graph1 ,该数字未排序.因此,我按照在此处,我根据深度对因子名称的级别进行了排序:

I got graph1 below which is not ordered. Therefore I followed the instructions found here, I ordered the levels of my factor name according to depth:

df2=df[order(df$depth),]
df2$name=factor(df2$name,levels=df2$name)
ggplot(df2,aes(x=factor(name),y=depth)) + geom_bar(stat='identity') + coord_flip() + labs(y='depth',x='species')

我在下面找到了 Graph2 .但是对我来说,下一步是根据 Mut 变量对条形进行不同的着色.

I got Graph2 below. But the next step for me was to color the bars differently depending on the Mut variable.

ggplot(df2,aes(x=factor(name),y=depth)) + geom_bar(stat='identity',data=subset(df2,df2$Mut==2),fill='red') + geom_bar(stat='identity',data=subset(df2,df2$Mut==1),fill='blue') + coord_flip() + labs(y='depth',x='species')

然后我得到了 Graph3 ,不再订购了!

And I got Graph3 which is not ordered anymore!!

如何根据graph2中显示的顺序生成graph3

                    name depth Mut            x
25  A_rubrocinctus_GA070     8   2 -0.033318659
9      A_omanensis_GA051    10   2 -0.020387101
4  A_latifasciatus_GA083    12   1 -0.005645811
27      A_frenatus_GA068    12   1 -0.024190876
13       A_percula_GA017    15   1  0.034591721
14       A_percula_GA039    15   2  0.034591721
15       A_percula_GA053    15   2  0.034591721
16     A_ocellaris_GA009    15   1  0.052042539
17     A_ocellaris_GA021    15   1  0.052042539
24     A_ephippium_GA057    15   2 -0.016859412
20   P_biaculeatus_GA008    16   1 -0.014466403
21   P_biaculeatus_GA025    16   1 -0.014466403
22   P_biaculeatus_GA065    16   1 -0.014466403
23     A_melanopus_GA034    18   2 -0.026915545
26     A_melanopus_GA012    18   2 -0.026915545
12  A_sandaracinos_GA018    20   1  0.055839755
6       A_nigripes_GA055    25   1  0.023420045
8          A_sebae_GA029    25   1  0.021767793
11   A_akallopisos_GA067    25   1  0.043272525
28   A_akallopisos_GA072    25   1  0.043272525
34     A_akindynos_GA032    25   1 -0.020707141
1       A_polymnus_GA004    30   1  0.030902254
3        A_allardi_GA033    30   1 -0.020277664
5      A_bicinctus_GA036    30   1 -0.025354572
7       A_polymnus_GA019    30   1  0.030902254
32  A_chrysopterus_GA040    30   1 -0.022402365
33  A_chrysopterus_GA031    30   1 -0.022402365
35   A_perideraion_GA020    38   1  0.052830132
36   A_perideraion_GA015    38   1  0.052830132
2     A_tricinctus_GA058    40   1 -0.016230301
18  A_chrysogaster_GA080    40   1  0.012608835
19  A_chrysogaster_GA077    40   1  0.012608835
10   A_latezonatus_GA023    45   1 -0.010718845
31    A_mccullochi_GA056    45   1 -0.031664307
29       A_clarkii_GA044    60   1 -0.014474658
30       A_clarkii_GA010    60   1 -0.014474658

图1 Graph2 Graph3 谢谢!

推荐答案

由于数据中有变量 Mut 可以确定每个观测值属于哪个级别,因此无需使用 geom_bar()两次,并带有子集.只需在 aes()内添加 fill = factor(Mut)并使用df2处理有序数据.条形将正确排列,并且颜色会自动生成.

As you have variable Mut in your data that determines to which level each observation belongs, you don't need to use geom_bar() twice with subset. Just add fill=factor(Mut) inside the aes() and use df2 with ordered data. Bars will be in correct order and color made automatically.

ggplot(df2,aes(x=factor(name),y=depth,fill=factor(Mut))) + 
     geom_bar(stat='identity') + 
     coord_flip() + labs(y='depth',x='species')

对于x值,原始数据帧 df aes()中的函数 reorder()可以实现相同的结果.

The same result can be achieved with original dataframe df and function reorder() inside aes() for x values.

ggplot(df,aes(x=reorder(name,depth),y=depth,fill=factor(Mut))) + 
     geom_bar(stat='identity') + 
     coord_flip() + labs(y='depth',x='species')

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

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