grouped bar graph

后端 未结 1 1676
滥情空心
滥情空心 2020-12-02 23:57

I have the following data:

bin groupname   total_dist
0   rowA    377
0   rowA    306.6
0   rowB    2.1
0   rowB    110.6
1   rowA    918.1
1   rowA    463.2         


        
相关标签:
1条回答
  • 2020-12-03 00:25

    Here is a classic solution. (Supposing your dataframe is named df )

    data <- tapply(df$total_dist, list(df$groupname,df$bin), sum)
    
    barplot(data,beside=T,col=c("#ee7700","#3333ff")
    ,main="European Parliament Elections",xlab="Group",ylab="Seats")
    
    legend(locator(1),rownames(data),fill=c("#ee7700","#3333ff"))
    

    and here is solution using ggplot2

    library(ggplot2)
    qplot(factor(bin),data=df,geom="bar",fill=groupname,weight=total_dist,position="dodge",
    main = "European Parliament Elections", xlab="Group",ylab="Seats")
    

    alt text

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