所以我的条形图遇到了这个问题,其中一个(两个中的)称为“竞争”的方面有一个空白条形空间,但这是因为其变量“其他”没有数据。
我需要找到一种方法来删除那个方面的空白,但我已经为此苦苦挣扎了很长时间了😭 下面是我的代码块中的 atm 内容,图片是我的条形图现在的样子。 任何帮助将不胜感激!
ggplot(mapping = aes(x = talent,y = mnAud,fill = talent)) +
theme_bw() +
geom_jitter(data=ds,mapping=aes(x=talent,y=mnAud,colour=talent), position = position_dodge(width = 0.9), alpha=0.5,show.legend=FALSE) +
scale_y_continuous(breaks=c(0, 5, 10)) +
geom_col(show.legend=FALSE,colour="black",alpha=0.5, stat="identity", position = position_dodge(width = 0.1)) +
geom_errorbar(mapping = aes(ymin = mnAud-sderrAud,
ymax = mnAud+sderrAud), width=0.2) +
facet_wrap(~level) +
scale_fill_brewer(palette="Set1") +
scale_colour_brewer(palette="Set1") +
labs(title = "Audience rating for each kind of talent",
y = "Rating (higher=better)",
x = "Talent")
我尝试过诸如位置=闪避和过滤之类的东西,但到目前为止我还没有成功。
Rui 的建议对我来说很有效,下面有一些虚构的数据和简化的代码。
如果您可以提供一个最小的可重现示例,包括您自己的示例数据,那就更好了,因为可能是其他原因阻止了
scales = "free_x"
达到所需的效果,例如您的 geom_jitter
参考文献 data = ds
但我们看不到其中的内容。
library(tidyverse)
df <- tribble(
~level, ~talent, ~mnAud,
"competitive", "comedy", 3,
"fun", "dancing", 2,
"competitive", "instrument", 3,
"fun", "magic", 2,
# "competitive", "other", 3, # remove other in one facet
"fun", "singing", 2,
"fun", "comedy", 3,
"competitive", "dancing", 2,
"fun", "instrument", 3,
"competitive", "magic", 2,
"fun", "other", 3,
"competitive", "singing", 2
)
# with the gap
df |> ggplot(aes(talent, mnAud, fill = talent)) +
geom_jitter(aes(talent, mnAud, colour = talent), show.legend = F) +
geom_col(show.legend = F) +
geom_errorbar(mapping = aes(ymin = mnAud - 0.1, ymax = mnAud + 0.1), width = 0.2) +
facet_wrap(~level)
# with scales = "free_x"
df |> ggplot(aes(talent, mnAud, fill = talent)) +
geom_jitter(aes(talent, mnAud, colour = talent), show.legend = F) +
geom_col(show.legend = F) +
geom_errorbar(mapping = aes(ymin = mnAud - 0.1, ymax = mnAud + 0.1), width = 0.2) +
facet_wrap(~level, scales = "free_x")
创建于 2024-04-14,使用 reprex v2.1.0