Draw histograms per row over multiple columns in R

前端 未结 2 1642
青春惊慌失措
青春惊慌失措 2020-12-06 20:13

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 X2         


        
相关标签:
2条回答
  • 2020-12-06 20:58

    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

    0 讨论(0)
  • 2020-12-06 21:12

    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

    0 讨论(0)
提交回复
热议问题