R ggplot排序栏中的“barplot-like”情节 [英] R ggplot ordering bars in "barplot-like " plot

查看:184
本文介绍了R ggplot排序栏中的“barplot-like”情节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CSV格式的此数据集并感谢来自stackoverflow的输入/帮助,我设法绘制了带有使用ggplot的组的彩色横条和条形的条形图!这是我第一次使用ggplot。

  GO生物过程,调节脂质代谢过程,1.87E-35 
GO生物过程,急性炎症反应, 3.21E-37
GO生物过程,对胰岛素刺激的反应,1.05E-38
GO生物过程,类固醇代谢过程,4.19E-39
GO生物过程,胆固醇代谢过程,1.19 E-40
GO生物过程,细胞对化学刺激的反应,5.87E-42
GO生物过程,酒精代谢过程,5.27E-43
GO生物过程,固醇代谢过程,2.61 E-43
GO生物过程,脂质体内平衡,1.12E-44
GO生物过程,对肽激素刺激的反应,1.29E-45
GO生物过程,单羧酸代谢过程,2.33 E-54
GO生物过程,细胞酮代谢过程,5.46E-74
GO生物过程,羧酸代谢过程,2.41E-76
GO生物过程,有机酸代谢过程, 5.30E-79
Pathway Commons,FOXA转录因子网络,7.40E-61
Pathway Commons,FOXA2和FOXA3转录因子网络,1.39E-64
转录因子靶标,通过肝细胞中的ChIP芯片鉴定的HNF6的靶标,1.77E- 32
转录因子靶标,通过肝细胞中的ChIP芯片鉴定的HNF1α的靶标,3.87E-65
转录因子靶标,通过肝细胞中的ChIP芯片鉴定的HNF4α的靶标,1.38 E-131

这是我的代码:



< pre $ ggplot(tmp,aes(x = tmp $ V2,y = -log10(tmp $ V3),fill = tmp $ V1))+
geom_bar(stat =身份)+
coord_flip()


现在我想创建与上面相同的图,但是值在每个组中进行排序。看起来像这样的东西。





我是ggplot的新手,所以任何帮助将不胜感激。
Thanks。



Ben

解决方案

通过将其转换为因子来变量。

 >头(d)
V1 V2 V3
1 GO生物过程调节脂质代谢过程1.87e-35
2 GO生物过程急性炎症反应3.21e-37
3 GO生物过程对胰岛素刺激的反应1.05e-38
4 GO生物过程类固醇代谢过程4.19e-39
5 GO生物过程胆固醇代谢过程1.19e-40
GO生物过程细胞反应化学刺激5.87e-42

> d $ V4 < - 因子(d $ V2,等级= d $ V2)#将V2转换为因子
>头(d)
V1 V2 V3 V4
1 GO生物过程调节脂质代谢过程1.87e-35调节脂质代谢过程
2 GO生物过程急性炎症反应3.21e-37急性炎症反应
3 GO生物过程对胰岛素刺激的反应1.05e-38对胰岛素刺激的反应
4 GO生物过程类固醇代谢过程4.19e-39类固醇代谢过程
GO生物过程胆固醇代谢过程1.19e-40胆固醇代谢过程
GO生物过程细胞对化学刺激的反应5.87e-42细胞对化学刺激的反应

> #plot
> ggplot(d,aes(V4,-log10(V3),fill = V1))+ geom_bar()+ coord_flip()

这里有更多信息: http://kohske.wordpress.com/2010/12/29/faq-how-to-order-the-factor-variables-in-ggplot2/


with this dataset in CSV format and thanks to input/help from stackoverflow I managed to plot a barplot with horizontal bars and bars colored by "groups" using ggplot ! This is the first time I use ggplot.

GO Biological Process,regulation of lipid metabolic process,1.87E-35
GO Biological Process,acute inflammatory response,3.21E-37
GO Biological Process,response to insulin stimulus,1.05E-38
GO Biological Process,steroid metabolic process,4.19E-39
GO Biological Process,cholesterol metabolic process,1.19E-40
GO Biological Process,cellular response to chemical stimulus,5.87E-42
GO Biological Process,alcohol metabolic process,5.27E-43
GO Biological Process,sterol metabolic process,2.61E-43
GO Biological Process,lipid homeostasis,1.12E-44
GO Biological Process,response to peptide hormone stimulus,1.29E-45
GO Biological Process,monocarboxylic acid metabolic process,2.33E-54
GO Biological Process,cellular ketone metabolic process,5.46E-74
GO Biological Process,carboxylic acid metabolic process,2.41E-76
GO Biological Process,organic acid metabolic process,5.30E-79
Pathway Commons,FOXA transcription factor networks,7.40E-61
Pathway Commons,FOXA2 and FOXA3 transcription factor networks,1.39E-64
Transcription Factor Targets,"Targets of HNF6, identified by ChIP-chip in hepatocytes",1.77E-32
Transcription Factor Targets,"Targets of HNF1alpha, identified by ChIP-chip in hepatocytes",3.87E-65
Transcription Factor Targets,"Targets of HNF4alpha, identified by ChIP-chip in hepatocytes",1.38E-131

This is my code:

ggplot(tmp, aes(x=tmp$V2, y=-log10(tmp$V3), fill=tmp$V1)) +
geom_bar(stat="identity") +
coord_flip()

Now I would like to create the same plot as above but where the values within each "groups" are sorted. Something that will look like this.

I am new to ggplot, so any help will be appreciated. Thanks.

Ben

解决方案

You can order the variable by converting it into factor.

> head(d)
                     V1                                     V2       V3
1 GO Biological Process  regulation of lipid metabolic process 1.87e-35
2 GO Biological Process            acute inflammatory response 3.21e-37
3 GO Biological Process           response to insulin stimulus 1.05e-38
4 GO Biological Process              steroid metabolic process 4.19e-39
5 GO Biological Process          cholesterol metabolic process 1.19e-40
6 GO Biological Process cellular response to chemical stimulus 5.87e-42

> d$V4 <- factor(d$V2, levels=d$V2) # convert V2 into factor
> head(d)
                     V1                                     V2       V3                                     V4
1 GO Biological Process  regulation of lipid metabolic process 1.87e-35  regulation of lipid metabolic process
2 GO Biological Process            acute inflammatory response 3.21e-37            acute inflammatory response
3 GO Biological Process           response to insulin stimulus 1.05e-38           response to insulin stimulus
4 GO Biological Process              steroid metabolic process 4.19e-39              steroid metabolic process
5 GO Biological Process          cholesterol metabolic process 1.19e-40          cholesterol metabolic process
6 GO Biological Process cellular response to chemical stimulus 5.87e-42 cellular response to chemical stimulus

> # plot
> ggplot(d, aes(V4, -log10(V3), fill=V1)) + geom_bar() + coord_flip()

here is further information: http://kohske.wordpress.com/2010/12/29/faq-how-to-order-the-factor-variables-in-ggplot2/

这篇关于R ggplot排序栏中的“barplot-like”情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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