【问题标题】:Plot table function objects with ggplot?用ggplot绘制表函数对象?
【发布时间】:2019-02-26 13:56:46
【问题描述】:

我有这些数据:

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 做到这一点?谢谢

【问题讨论】:

  • Ggplot2 最适合“长”格式的日期,您的表格在 wide 中。你能提供来自maindput 吗?
  • @Wimpel 亲爱的,您能否详细说明您需要什么我是 R 新手。谢谢
  • @wimpel table 类已经很长了,它只是以一种特殊的方式打印。如果你用as.data.frame传递它会很长。

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


【解决方案1】:
ggplot(as.data.frame(table(main$Sex1,main$District)), aes(Var1, Freq, fill=Var2)) + 
  geom_bar(stat="identity")

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

【讨论】:

  • 我发现这也有效 谢谢 p
  • 是的,这是更直接的方法 :) 您可以将“main$”放在aes 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-24
  • 2021-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多