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

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

有帮助吗?

解决方案

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")

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")

enter image description here

其他提示

Here's a solution in lattice:

require(lattice)
barchart(X2007+X2008+X2009+X2010+X2011+X2012 ~ HEI.ID,
         data=HEIrank11,
         auto.key=list(space='right')
         )

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top