提问人:Crunchysoups 提问时间:10/28/2023 最后编辑:Crunchysoups 更新时间:10/28/2023 访问量:29
在facet_grid中苦苦挣扎,包括正确的属性
Struggling with facet_grid including correct properties
问:
我有一个数据集,每个主要类别(“行踪”)的子类别(“事物”)数量不等。我想构建一个条形图,在 x 轴上显示子类别,按主要类别分组。但是,我一直遇到一个问题,即我的所有子类别都分组到条形图上的每个类别中。
当我生成箱形图时,我可以得到正确的轴:
SpookyReport <- HalloweenData %>% mutate(Whereabouts = fct_relevel(Whereabouts, c("Dead", "Halfdead", "Alive"))) %>%
ggplot(HalloweenData, mapping = aes(y = Totalspook, x = Whereabouts, fill = Thingies)) +
geom_boxplot() +
scale_fill_manual(values=SpookyPalette) +
labs(y="Total spook (spook-o-meters)", x=NULL) + ggtitle("Effect of losing my mind over this") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position="right") +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
theme(text = element_text(size = 15))
SpookyReport
但是,当我尝试使用以下代码生成条形图时:
SpookySumReport2<-ddply(HalloweenData, .(Thingies), summarise, .progress="text", mean.cm=mean(Totalspook),
SD.cm=sd(Totalspook),SE.cm=sd(Totalspook)/sqrt(length(Totalspook)))
SpookySumReport2
SpookyReport2 <- HalloweenData %>% mutate(Whereabouts = fct_relevel(Whereabouts, c("Dead", "Halfdead", "Alive"))) %>%
ggplot(LiveInocsContrasts, mapping = aes(x=Thingies, y=Totalspook, fill= Thingies)) +
geom_bar(stat='summary') +
geom_beeswarm(aes(x = Thingies, y = Totalspook), dodge.width = 0.9, alpha = 0.5)+
geom_errorbar(data = SpookySumReport2, aes(x = Thingies, y = mean.cm, ymin=mean.cm-SE.cm,ymax=mean.cm+SE.cm, colour=Thingies),
color = 'black', width = 0.2, position = position_dodge(width = 0.9)) +
scale_fill_manual(values=SpookyPalette) +
facet_grid(~ Whereabouts,
scales = "free_x",
space = "free_x",
switch = "x") +
theme(panel.spacing = unit(0, units = "cm"),
strip.placement = "outside",
strip.background = element_rect(fill = "white")) +
theme(legend.position="none") +
labs(y="Total spook (spook-o-meters)", x=NULL) + ggtitle("Effect of losing my mind over this") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
theme(text = element_text(size = 15))
SpookyReport2
我得到这样的东西:糟糕的条形图
正如你所看到的,所有子类别都存在于主要类别中,但这不是我的数据在我的数据集中的分组方式。这是我第一次与facet_grid合作,所以也许我做错了什么?谁能找出问题所在?非常感谢!
答: 暂无答案
评论