在R中的多列上每行绘制直方图 [英] Draw histograms per row over multiple columns in R

查看:363
本文介绍了在R中的多列上每行绘制直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R来分析我的硕士论文
我有以下数据框:STOF:学生与工作人员的比例

I'm using R for the analysis of my master thesis I have the following data frame: STOF: Student to staff ratio

    HEI.ID   X2007 X2008 X2009 X2010 X2011 X2012 
1        OP  41.8 147.6  90.3  82.9 106.8  63.0    
2        MO  20.0  20.8  21.1  20.9  12.6  20.6    
3        SD  21.2  32.3  25.7  23.9  25.0  40.1    
4        UN  51.8  39.8  19.9  20.9  21.6  22.5    
5        WS  18.0  19.9  15.3  13.6  15.7  15.2    
6        BF  11.5  36.9  20.0  23.2  18.2  23.8    
7        ME  34.2  30.3  28.4  30.1  31.5  25.6    
8        IM   7.7  18.1  20.5  14.6  17.2  17.1    
9        OM  11.4  11.2  12.2  11.1  13.4  19.2    
10       DC  14.3  28.7  20.1  17.0  22.3  16.2    
11       OC  28.6  44.0  24.9  27.9  34.0  30.7    

然后我使用此命令对大学进行排名

Then I rank colleges using this commend

HEIrank1<-(STOF[,-c(1)])
rank1 <- apply(HEIrank1,2,rank)

> HEIrank11
     HEI.ID X2007 X2008 X2009 X2010 X2011 X2012
1        OP  18.0    20  20.0  20.0  20.0    20
2        MO  14.0     9  13.0  13.5   2.0    12
3        SD  15.0    16  17.0  16.0  16.0    19
4        UN  20.0    18   8.0  13.5  14.0    13
5        WS  12.0     8   4.0   7.0   6.0     8
6        BF   6.5    17   9.5  15.0  10.0    14
7        ME  17.0    15  19.0  19.0  17.0    15
8        IM   2.0     6  12.0   8.0   8.5    10
9        OM   4.5     3   2.5   3.0   3.0    11
10       DC  11.0    14  11.0   9.0  15.0     9
11       OC  16.0    19  16.0  18.0  19.0    17

我想为每个HEI(每行)绘制直方图吗?

I would like to draw histogram for each HEIs (for each row)?

推荐答案

如果您使用ggplot,则无需将其作为循环执行,可以一次绘制所有图形。另外,您需要重新格式化数据,以使其采用长格式而不是短格式。您可以使用reshape包中的melt函数。

If you use ggplot you won't need to do it as a loop, you can plot them all at once. Also, you need to reformat your data so that it's in long format not short format. You can use the melt function from the reshape package to do so.

library(reshape2)
new.df<-melt(HEIrank11,id.vars="HEI.ID")
names(new.df)=c("HEI.ID","Year","Rank")

子字符串每年都摆脱X

substring is just getting rid of the X in each year

library(ggplot2)
ggplot(new.df, aes(x=HEI.ID,y=Rank,fill=substring(Year,2)))+
   geom_histogram(stat="identity",position="dodge")

这篇关于在R中的多列上每行绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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