r - 带填充的 R 条形图
问题描述
我正在尝试使用填充创建一个条形图以在一个条形图中显示多个值。我能够得到每个 x 值的总数(y 值),但是当我尝试时,fill
我只得到计数。
假设示例数据是一个名为的数据框data
:
State Num.Class Num.Tweets
Pennsylvania C 98
Pennsylvania P 10
Pennsylvania E 174
Pennsylvania S 70
Texas C 233
Texas P 42
Texas E 30
Texas S 26
California C 57
California P 32
California E 39
California S 20
Massachusetts C 23
Massachusetts P 74
Massachusetts E 1
Massachusetts S 3
使用ggplot
我可以得到总数:
ggplot(data) + aes(x=State, y = Num.Tweets) + geom_col()
当我尝试fill
使用gf_bar
orggplot() + geom_bar
我得到这个而不是一个小节内的四个计数时:
gf_bar(~ State, fill =~ Num.Tweets, data = data)
# or
ggplot()+
geom_bar(aes(x = data$Num.Class, fill = data$Num.Tweets), color = "black" , position = "stack", show.legend = FALSE)
我一直在看这篇文章和其他文章,但没有看到我错过了什么。如果我移动变量,我会得到错误或没有输出。
例如:
gf_bar(Num.Tweets ~ State, fill =~ Num.Class, data = data)
返回Error: stat_count() must not be used with a y aesthetic.
错误。
解决方案
推荐阅读
- android - 使用recyclerview在android中不水平显示表格布局表格行内容
- php - Laravel 多张产品图片上传
- python - Python3:打印带有特殊字符的字符串
- c# - iText 7表格的底部边框未绘制
- ionic-framework - FCM 使用推送通知 ionic 4
- reactjs - AgGrid:如何根据变化以不同颜色闪烁单元格
- bash - 如何将输出从一个文件夹重定向到另一个文件夹?
- ms-word - Office-JS Word 插件文档到侧边栏通信
- sql-server - 在循环中合并 n 次出现的两个数据行
- ios - 应用程序未上传到 iOS 应用程序连接商店,原因是非公共 API