用ggplot绘制表函数对象?

时间:2018-09-22 10:43:28

标签: r ggplot2 plot bar-chart geom-bar

我有以下数据:

table(main$Sex1,main$District)

        Bahawalnagar Barkhan Chiniot  Faisalabad Ghotki 
Female    37           16       26       97         46          
Male      25           19       15       20         25 

我可以用底数R绘制它

barplot(table(main$Sex1,main$District))

所以我的问题是,如何使用ggplot2做到这一点?谢谢

1 个答案:

答案 0 :(得分:2)

ggplot(as.data.frame(table(main$Sex1,main$District)), aes(Var1, Freq, fill=Var2)) + 
  geom_bar(stat="identity")

table类很长,但是它以一种特殊的方式打印,并且ggplot不知道如何处理它。如果将它与as.data.frame一起传递,则可以通过ggplot进行完美管理。