使用 ggplot 绘制 table 函数对象?

Plot table function objects with ggplot?

我有这个数据:

table(main$Sex1,main$District)

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

我可以用 base R 绘制它

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

所以我的问题是,如何使用 ggplot2 执行此操作?谢谢

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

table class 很长,但是打印的方式比较特殊,ggplot 不知道怎么处理。如果你用 as.data.frame 传递它,ggplot 将完全可以管理它。