Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot打印表函数对象?_R_Ggplot2_Plot_Bar Chart_Geom Bar - Fatal编程技术网

R 使用ggplot打印表函数对象?

R 使用ggplot打印表函数对象?,r,ggplot2,plot,bar-chart,geom-bar,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$

我得到了以下数据:

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

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

table类很长,但它以一种特殊的方式打印,而ggplot不知道如何处理它。如果您使用as.data.frame传递它,它将完全可以通过ggplot进行管理

Ggplot2最适用于“长”格式的日期,表格的宽度为
wide
。你能从
main
提供一个
dput
吗?@Wimpel亲爱的,你能详细说明一下你需要什么吗我是R的新手。谢谢阅读:@Wimpel
table
类已经很长了,它只是以一种特殊的方式打印。如果您将它与
作为.data.frame传递,它将很长。我发现这也起作用,谢谢p是的,这是更简单的方法:)您可以将“main$”放在
aes
中。