R 分组条形图,一个数值变量与三个阶乘变量
我有以下问题。我需要绘制3个因子变量和1个数值变量的条形图R 分组条形图,一个数值变量与三个阶乘变量,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我有以下问题。我需要绘制3个因子变量和1个数值变量的条形图 ggplot(dat, aes(Status, Count, fill = Gall)) + geom_bar(stat = "identity", position = "dodge") 我的数据集: Site,Gall,Status,Count Site1,absent,unhealthy,35 Site1,absent,healthy,1750 Site1,present,unhealthy,23 Site1,present
ggplot(dat, aes(Status, Count, fill = Gall)) +
geom_bar(stat = "identity", position = "dodge")
我的数据集:
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")
有人能帮我吗
谢谢,非常感谢这里有几个解决方案。如果您打算按两个因素填充,则可以使用
交互
:
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 ~ .)
library(dplyr)
library(ggplot2)
ggplot(dat %>% mutate(Site = gsub("([0-9]$)", " \\1", Site)),
aes(Status, Count, colour=Status, shape=Gall)) +
geom_point(size=3, position=position_dodge(0.5), stroke=1) +
facet_grid(~ Site, switch="x") +
theme_classic() +
theme(strip.placement = "outside",
strip.background=element_blank()) +
scale_colour_manual(values=hcl(c(195,15),100,65)) +
scale_shape_manual(values=c(1,16)) +
labs(x="") +
guides(colour=FALSE)
你最好用点而不是条。例如:
ggplot(dat, aes(Status, Count)) +
geom_col(aes(fill = Gall), position = "dodge") + facet_grid(Site ~ .)
library(dplyr)
library(ggplot2)
ggplot(dat %>% mutate(Site = gsub("([0-9]$)", " \\1", Site)),
aes(Status, Count, colour=Status, shape=Gall)) +
geom_point(size=3, position=position_dodge(0.5), stroke=1) +
facet_grid(~ Site, switch="x") +
theme_classic() +
theme(strip.placement = "outside",
strip.background=element_blank()) +
scale_colour_manual(values=hcl(c(195,15),100,65)) +
scale_shape_manual(values=c(1,16)) +
labs(x="") +
guides(colour=FALSE)
您可以在映射中添加类似于
group=Site
的内容,以包括第三个因素。虽然facet\u wrap
可能是呈现大量信息的更简洁的方式。@AdamQuek-这非常有用,我已经将facet\u wrap用于factor站点,它看起来正是我想要的!我非常感谢,谢谢你的时间,伙计!谢谢大家,我没想到会有这么快、详细和有用的答案!我第一次使用这个论坛,真是太棒了。