使用ggplot绘制表格函数对象?

5

我有这些数据:

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实现这一点?谢谢。

Ggplot2 最适合使用“长格式”中的日期,而您的表格是“宽格式”。您能否提供来自“main”的“dput”? - Wimpel
@Wimpel 亲爱的,你能否详细说明你需要什么?我对R还不熟悉。谢谢。 - Ali Zohaib
1
阅读:https://dev59.com/eG025IYBdhLWcg3whGSx - Wimpel
2
@wimpel table类已经很长了,它以一种特殊的方式打印。如果你使用as.data.frame传递它,它会变得更长。 - iod
1个回答

4
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就可以完美地处理它。


我发现这也行。谢谢。 p <- ggplot(main, aes(main$District)) p + geom_bar(aes(fill = Sex1)) - Ali Zohaib
1
是的,那是更直接的方法 :) 你可以在 aes 中省略 "main$"。 - iod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接