简体   繁体   English

在R中绘制数据帧的分组值

[英]Plot grouped values of a dataframe in R

I have the following dataframe: 我有以下数据框:

          stat                     mTADs                    DE_genes
    5267 -5.452819  chr2:167337500-167447500  chr2:167318145-167341673:+
    5268  4.114012    chr6:41532500-41642500    chr6:41555481-41570508:+
    5269  9.812369   chr10:18157500-18262500   chr10:18259929-18265882:-
    5270  3.371969   chr17:40957500-41062500   chr17:41060000-41071996:-
    5271  4.576930   chr17:40957500-41062500   chr17:41012431-41017507:-
    5272  2.952151   chr11:72251250-72352500   chr11:72254857-72265270:+
    5273 -3.349795  chr1:174307500-174407500  chr1:174405489-174408706:+
    5274 -2.685897 chr13:100777500-100877500 chr13:100787949-100874025:-
    5275  2.865269 chr13:100777500-100877500 chr13:100718488-100785594:-
    5276  6.436959  chr4:150417500-150517500  chr4:150377761-150418774:-
    5277  2.622196      chr7:6072500-6162500      chr7:6123828-6142951:+
    5278 -5.605531   chr11:48597500-48682500   chr11:48675470-48685185:-
    5279  3.554733   chr11:48597500-48682500   chr11:48639642-48665711:+
    5280  4.399655   chr11:48597500-48682500   chr11:48638848-48640157:-

As you see some DE_genes fall within the same mTAD. 如您所见,一些DE_genes属于同一mTAD。 I would like to plot for all the DE_genes their stat values and group them by mTAD. 我想为所有DE_genes绘制其统计值,并按mTAD进行分组。 I thought of doing this as a horizontal barplot having on the yaxis the genes, and on the x axis the stat values and group them by TAD but first i do not know how to do it and second i thought that a heatmap might be a better option. 我考虑将其作为水平条形图在y轴上具有基因,在x轴上具有stat值并将它们按TAD分组,但首先我不知道如何做,其次我认为热图可能更好选项。 Is there any way to do that in R? 有没有办法在R中做到这一点? In total I have 1700 mTADs and I would like to see if there are any patterns in the data. 我总共有1700个mTAD,我想看看数据中是否有任何模式。

Many thanks, Dimitris 非常感谢Dimitris

You might consider a sorted dotchart instead of a barchart. 您可能会考虑使用排序的点图而不是条形图。

> thing
     ID      stat                     mTADs                    DE_genes
1  5267 -5.452819  chr2:167337500-167447500  chr2:167318145-167341673:+
2  5268  4.114012    chr6:41532500-41642500    chr6:41555481-41570508:+
3  5269  9.812369   chr10:18157500-18262500   chr10:18259929-18265882:-
4  5270  3.371969   chr17:40957500-41062500   chr17:41060000-41071996:-
5  5271  4.576930   chr17:40957500-41062500   chr17:41012431-41017507:-
6  5272  2.952151   chr11:72251250-72352500   chr11:72254857-72265270:+
7  5273 -3.349795  chr1:174307500-174407500  chr1:174405489-174408706:+
8  5274 -2.685897 chr13:100777500-100877500 chr13:100787949-100874025:-
9  5275  2.865269 chr13:100777500-100877500 chr13:100718488-100785594:-
10 5276  6.436959  chr4:150417500-150517500  chr4:150377761-150418774:-
11 5277  2.622196      chr7:6072500-6162500      chr7:6123828-6142951:+
12 5278 -5.605531   chr11:48597500-48682500   chr11:48675470-48685185:-
13 5279  3.554733   chr11:48597500-48682500   chr11:48639642-48665711:+
14 5280  4.399655   chr11:48597500-48682500   chr11:48638848-48640157:-

First we will take medians by mTADs. 首先,我们将以mTAD为中位数。

medians.of.stat.by.mTADs<-aggregate(stat~mTADs,data=thing,FUN=median)
names(medians.of.stat.by.mTADs)[2]<-"median stat for mTAD"

Now merge these medians with the original data frame and create a factor by ordering on the sorted stat values. 现在,将这些中值与原始数据框合并,并通过对排序的stat值进行排序来创建一个因子。

thing<-merge(thing,medians.of.stat.by.mTADs,all.x = T,by="mTADs")
thing$mTADs.reordered <-factor(thing$mTADs, levels=thing[order(thing$`median stat for mTAD`), "mTADs"])

This will throw a warning because of duplicated levels but it seems to work. 由于级别重复,这将引发警告,但它似乎可以工作。

> thing
                       mTADs   ID      stat                    DE_genes median stat for mTAD           mTADs.reordered
1   chr1:174307500-174407500 5273 -3.349795  chr1:174405489-174408706:+            -3.349795  chr1:174307500-174407500
2    chr10:18157500-18262500 5269  9.812369   chr10:18259929-18265882:-             9.812369   chr10:18157500-18262500
3    chr11:48597500-48682500 5278 -5.605531   chr11:48675470-48685185:-             3.554733   chr11:48597500-48682500
4    chr11:48597500-48682500 5279  3.554733   chr11:48639642-48665711:+             3.554733   chr11:48597500-48682500
5    chr11:48597500-48682500 5280  4.399655   chr11:48638848-48640157:-             3.554733   chr11:48597500-48682500
6    chr11:72251250-72352500 5272  2.952151   chr11:72254857-72265270:+             2.952151   chr11:72251250-72352500
7  chr13:100777500-100877500 5274 -2.685897 chr13:100787949-100874025:-             0.089686 chr13:100777500-100877500
8  chr13:100777500-100877500 5275  2.865269 chr13:100718488-100785594:-             0.089686 chr13:100777500-100877500
9    chr17:40957500-41062500 5270  3.371969   chr17:41060000-41071996:-             3.974449   chr17:40957500-41062500
10   chr17:40957500-41062500 5271  4.576930   chr17:41012431-41017507:-             3.974449   chr17:40957500-41062500
11  chr2:167337500-167447500 5267 -5.452819  chr2:167318145-167341673:+            -5.452819  chr2:167337500-167447500
12  chr4:150417500-150517500 5276  6.436959  chr4:150377761-150418774:-             6.436959  chr4:150417500-150517500
13    chr6:41532500-41642500 5268  4.114012    chr6:41555481-41570508:+             4.114012    chr6:41532500-41642500
14      chr7:6072500-6162500 5277  2.622196      chr7:6123828-6142951:+             2.622196      chr7:6072500-6162500

Now do a simple dotchart. 现在做一个简单的点图。

ggplot() + geom_point(data=thing, aes(x=stat,y=mTADs.reordered), shape=20, cex=3.3)

按mTAD的中位数对点图进行排序

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

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