我有以下问题。我需要对 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")
但总的来说,最好对多个因素使用分面。例如:
ggplot(dat, aes(Status, Count)) +
geom_col(aes(fill = Gall), position = "dodge") + facet_grid(Site ~ .)
关于r - 分组条形图,一个数值变量与三个因子变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43839625/