RLo*_*ver 5 r bar-chart ggplot2
我遇到以下问题.我需要绘制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
Run Code Online (Sandbox Code Playgroud)
我尝试过使用ggplot,但是它只允许我定义x,y和另外一个选项,所以我使用了fill = Gall.
我的代码看起来如下,我仍然缺少一个因子变量.
ggplot(dat, aes(Status, Count, fill = Gall)) +
geom_bar(stat = "identity", position = "dodge")
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?
谢谢,非常感谢
有几种解决方案。如果您打算通过两个因素来填充,您可以使用interaction:
ggplot(dat, aes(Status, Count)) +
geom_col(aes(fill = interaction(Site, Gall)), position = "dodge")
Run Code Online (Sandbox Code Playgroud)
但一般来说,针对多个因素最好使用分面。例如:
ggplot(dat, aes(Status, Count)) +
geom_col(aes(fill = Gall), position = "dodge") + facet_grid(Site ~ .)
Run Code Online (Sandbox Code Playgroud)