4

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

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
Safiya Kkmha
  • 55
  • 1
  • 5
  • I try commend OP<-rank1[1,] hist(OP,breaks=3) I want to draw histogram as loop – Safiya Kkmha Mar 08 '14 at 03:58
  • Each row only has 6 values. Are you sure it even makes sense to draw a histogram from 6 values? Perhaps you means that you want a histogram for each numeric column? – joran Mar 08 '14 at 04:15
  • I want for example (OP) college get rank number (20) 5 times.SO X should be ranks and Y should be frequency. I need help.. – Safiya Kkmha Mar 08 '14 at 04:34

2 Answers2

3

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

  • Hi crmhaske....I try to run the last commends but shows an error..I do not why and i am not good in R.The error is ...(Error in rename(x, .base_to_ggplot, warn_missing = FALSE) : could not find function "revalue") so can you help what mistake I did??thank you – Safiya Kkmha Mar 08 '14 at 23:10
  • The function revalue is in the plyr package. It's probably a dependency you don't have. Do `install.packages("plyr")` and then `library(plyr)`. It should work after that. – Christie Haskell Marsh Mar 09 '14 at 00:14
  • Hi crmhaske I try to plot histogram for each college as my adviser need it using par( mfrow = c( 2, 2 ) ) hist(as.numeric(HEIrank11[1,-1]),nclass=12) I want name of college appear in graph.can you help me – Safiya Kkmha Mar 10 '14 at 06:19
  • Your histogram code doesn't make sense. You can't have a negative index. I'm not sure exactly what you are trying to plot and how it's different from what I and Matthew showed you using ggplot2 and lattice? – Christie Haskell Marsh Mar 10 '14 at 12:51
1

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

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
  • Hi Matthew....I try to run the commend but shows an error..I do not why and I am not good in R.The error is ...(Error: could not find function "barchart").Can you help me. – Safiya Kkmha Mar 08 '14 at 23:16