【发布时间】:2022-01-01 10:53:18
【问题描述】:
我使用ggplot2 创建了一个条形图,但我的一些条形图合并在一起。这是我使用的代码:
library(ggplot2)
library(RColorBrewer)
bird_plot_error <- test %>%
group_by(bird) %>%
summarise(key = unique(dr),
dr = sum(dr)) %>%
group_by(bird) %>%
mutate(dr = dr/sum(dr) * key) %>%
ungroup %>%
mutate(bird = fct_reorder(bird, desc(bird))) %>%
ggplot(aes(x=bird, y=dr, fill="lightblue")) +
geom_bar(stat="identity")+
theme_classic() +
theme(legend.position="none")+
coord_flip() +
scale_fill_brewer(palette="Paired")+
ylim(0, 0.8)
bird_plot_error <- bird_plot_error + ggtitle("Detection rate by bird") +
xlab("Bird") + ylab("Detection rate")
这是输出:
为什么我的两个条在图表中心合并在一起?有没有办法解决这个问题?
【问题讨论】:
-
因为你有很多条,R 可能会压缩它们,因为随着条数的增加,它倾向于删除条之间的空间。尝试仅绘制合并的条形图,看看它们是否仍然合并。
-
请提供一个可重现的示例,包括数据,大概是
test使用dput(test)或重现问题的较小子集,这将有助于理解和解决问题。 -
尝试调整
geom_bar()的“宽度”参数。