使用ggplot2订购分组条形图

时间:2018-04-25 11:00:39

标签: r ggplot2 bar-chart

我正在尝试将一些按两个变量排序的数据可视化:年份和学生。这是我的数据:

graphos <- data.frame(c(2007,2007,2007,2008,2008,2008,2009,2009,2009),
                      c("A","B","C","A","E","B","D","B","A"),
                      c(52080,49426,42522,53124,50784,45920,55971,52216,49850))

names(graphos) <- c("Year","Classroom","Students")

graph2 <- ggplot(graphos, aes(fill=Classroom, y=Students, x=Year)) + 
  geom_bar(position="dodge", stat="identity", color='black')

graph2

结果如下:

我尝试了fill=reorder的选项,但它也不起作用:

graph2 <- ggplot(graphos, aes(fill=reorder(Classroom, -Students), y=Students, x=Year)) + 
  geom_bar(position="dodge", stat="identity", color='black')

graph2

我需要的是按年份(升序)和学生(降序)排序,但我的图表只按年份排序。你能救我吗?

谢谢!

0 个答案:

没有答案