r - 分组条形图,一个数值变量与三个因子变量

标签 r ggplot2 bar-chart

我有以下问题。我需要对 3 个因子变量与 1 个数值变量进行条形图绘制。

我的数据集:

Site,Gall,Status,Count
Site1,absent,unhealthy,35
Site1,absent,healthy,1750
Site1,present,unhealthy,23
Site1,present,healthy,1146
Site2,absent,unhealthy,146
Site2,absent,healthy,1642
Site2,present,unhealthy,30
Site2,present,healthy,333

我曾尝试使用 ggplot,但它只允许我定义 x、y 和另一个选项,所以我使用了 fill=Gall。

我的代码如下所示,我仍然缺少一个因子变量。
ggplot(dat, aes(Status, Count, fill = Gall)) +
  geom_bar(stat = "identity", position = "dodge")

有人可以帮我吗?

谢谢,非常感谢

最佳答案

有几种解决方案。如果您打算通过两个因素进行填充,则可以使用 interaction :

ggplot(dat, aes(Status, Count)) + 
  geom_col(aes(fill = interaction(Site, Gall)), position = "dodge")

enter image description here

但总的来说,最好对多个因素使用分面。例如:
ggplot(dat, aes(Status, Count)) + 
  geom_col(aes(fill = Gall), position = "dodge") + facet_grid(Site ~ .)

enter image description here

关于r - 分组条形图,一个数值变量与三个因子变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43839625/

相关文章:

r - 如何在同一图上叠加 2 条正态分布曲线?

gnuplot - gnuplot 不显示数据框

r - 从现有的ggplot图表中删除geom?

r - 从ggplot2中提取颜色信息?

r - 避免使用 ggtern 与 ggplot2 和 directlabels 重叠标签

python - 更改基本条形图的宽度

更新数据时,Javafx 条形图 X 轴无法正确缩放

r - 使用R中的重复标识符将数据帧从长转换为宽

r - R 决策树

r - 数据框名称 R