根据其大小(即数值)对ggplot2排序条形图 [英] Ordering bar plots with ggplot2 according to their size, i.e. numerical value

查看:1130
本文介绍了根据其大小(即数值)对ggplot2排序条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题询问根据未整理的表格订购条形图。我有一个稍微不同的情况。以下是我原始数据的一部分:

 实验,pvs_id,src,hrc,mqs,mcs,dmqs,imcs 
dna-wm,0,7,9,4.454545454545454,1.4545454545454546,1.4545454545454541,4.3939393939393945
dna-wm,1,7,4,2.909090909090909,1.8181818181818181,0.09090909090909083,3.9090909090909087
dna-wm,2,7, 1,4.818181818181818,1.4545454545454546,1.8181818181818183,4.3939393939393945
dna-wm,3,7,8,3.4545454545454546,1.5454545454545454,0.4545454545454546,4.272727272727273
dna-wm,4,7,10,3.8181818181818183,1.9090909090909092,0.8181818181818183, 3.7878787878787876
dna-wm,5,7,7,3.909090909090909,1.9090909090909092,0.9090909090909092,3.7878787878787876
dna-wm,6,7,0,4.909090909090909,1.3636363636363635,1.9090909090909092,4.515151515151516
dna-wm ,7,7,3,3.909090909090909,1.7272727272727273,0.9090909090909092,4.030303030303029
dna-wm,8,7,11,3.6363636363636362,1.5454545454545454,0.6363636363636362,4.272727272727273

我只需要几个var其中的 c $ c> mqs 和 imcs ,按照它们的 pvs_id

  m = melt(t,id.var =pvs_id,measure。 var = c(mqs,imcs))

我可以将其作为条形图在那里你可以看到 MQS IMCS 之间的相关性。

  ggplot (m,aes(x = pvs_id,y = value))
+ geom_bar(aes(fill = variable),position =dodge,stat =identity)



但是,我希望结果栏由 MQS 按从左到右的顺序排序。 IMCS 值应当与那些排序。



我该如何实现这一点?一般来说,给定任何熔化的数据框 - 这对于ggplot2中的图形显示非常有用,而今天我第一次偶然发现它 - 如何指定一个变量的顺序?

解决方案

这一切都在制作



pvs_id

  dat $ pvs_id<  -  factor(dat $ pvs_id,levels = dat [order(-dat $ mqs ),2])

m = melt(dat,id.var =pvs_id,measure.var = c(mqs,imcs))

ggplot (m,aes(x = pvs_id,y = value))+
geom_bar(aes(fill = variable),position =dodge,stat =identity)



产生以下图表:


编辑:
c> pvs_id
是数字,它以有序方式处理。如果你有一个因素,没有订单。所以即使你有数字标签 pvs_id 实际上是一个因素(标称值)。就 dat [order(-dat $ mqs),2] 而言,具有负号的顺序函数将数据帧从变量 MQS 。但是您对 pvs_id 变量的顺序感兴趣,因此您需要索引第二列的该列。如果你把它分开,你会看到它给你:

 > dat [order(-dat $ mqs),2] 
[1] 6 2 0 5 7 4 8 3 1

现在你将它提供给 factor levels 参数,想要它。


This question asks about ordering a bar graph according to an unsummarized table. I have a slightly different situation. Here's part of my original data:

experiment,pvs_id,src,hrc,mqs,mcs,dmqs,imcs
dna-wm,0,7,9,4.454545454545454,1.4545454545454546,1.4545454545454541,4.3939393939393945
dna-wm,1,7,4,2.909090909090909,1.8181818181818181,0.09090909090909083,3.9090909090909087
dna-wm,2,7,1,4.818181818181818,1.4545454545454546,1.8181818181818183,4.3939393939393945
dna-wm,3,7,8,3.4545454545454546,1.5454545454545454,0.4545454545454546,4.272727272727273
dna-wm,4,7,10,3.8181818181818183,1.9090909090909092,0.8181818181818183,3.7878787878787876
dna-wm,5,7,7,3.909090909090909,1.9090909090909092,0.9090909090909092,3.7878787878787876
dna-wm,6,7,0,4.909090909090909,1.3636363636363635,1.9090909090909092,4.515151515151516
dna-wm,7,7,3,3.909090909090909,1.7272727272727273,0.9090909090909092,4.030303030303029
dna-wm,8,7,11,3.6363636363636362,1.5454545454545454,0.6363636363636362,4.272727272727273

I only need a few variables from this, namely mqs and imcs, grouped by their pvs_id, so I create a new table:

m = melt(t, id.var="pvs_id", measure.var=c("mqs","imcs"))

I can plot this as a bar graph where one can see the correlation between MQS and IMCS.

ggplot(m, aes(x=pvs_id, y=value)) 
+ geom_bar(aes(fill=variable), position="dodge", stat="identity")

However, I'd like the resulting bars to be ordered by the MQS value, from left to right, in decreasing order. The IMCS values should be ordered with those, of course.

How can I accomplish that? Generally, given any molten dataframe — which seems useful for graphing in ggplot2 and today's the first time I've stumbled over it — how do I specify the order for one variable?

解决方案

It's all in making

pvs_id a factor and supplying the appropriate levels to it:

dat$pvs_id <- factor(dat$pvs_id, levels = dat[order(-dat$mqs), 2])

m = melt(dat, id.var="pvs_id", measure.var=c("mqs","imcs"))

ggplot(m, aes(x=pvs_id, y=value))+ 
    geom_bar(aes(fill=variable), position="dodge", stat="identity")

This produces the following plot:

EDIT: Well since pvs_id was numeric it is treated in an ordered fashion. Where as if you have a factor no order is assumed. So even though you have numeric labels pvs_id is actually a factor (nominal). And as far as dat[order(-dat$mqs), 2] is concerned the order function with a negative sign orders the data frame from largest to smallest along the variable mqs. But you're interested in that order for the pvs_id variable so you index that column which is the second column. If you tear that apart you'll see it gives you:

> dat[order(-dat$mqs), 2]
[1] 6 2 0 5 7 4 8 3 1

Now you supply that to the levels argument of factor and this orders the factor as you want it.

这篇关于根据其大小(即数值)对ggplot2排序条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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