Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

drawing - Draw histograms per row over multiple columns in R

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...