[英]How to centre single bar position with multiple bars in position_dodge in ggplot2
我有以下 geom_bar 闪避 plot 并认为 8、17、26 和 27 岁的单条看起来更好集中而不是向左倾斜。 我不确定要在脚本中添加什么来实现这一点。 任何帮助将不胜感激。
这是脚本:
ggplot(data = combo1, aes(x = Age_Year, fill = Tactic)) +
geom_bar(position = position_dodge(preserve = 'single')) +
theme_classic() +
labs(x = "Age (years)", y = "Counts of Fish", show.legend = FALSE)+
theme(legend.position = "none")+
scale_fill_manual("legend", values = c("Migr" = "skyblue", "OcRes" = "pale green", "EstRes" = "pink"))
OP,使用position_dodge2(preserve="single")
代替position_dodge(preserve="single")
。 出于某种原因,居中条形/列不能与position_dodge()
完全正常工作,但它可以与position_dodge2()
一起工作。 请注意切换 position function 时的间距略有不同,但总体上应该可以解决您的问题。
library(ggplot2)
set.seed(8675309)
df <- data.frame(
x=c("A", "A", "A", "B", "C", "C"),
grouping_var = c("Left", "Middle", "Right", "Middle", "Left", "Right"),
values = sample(1:100, 6))
基本 plot 与position_dodge()
:
ggplot(df, aes(x=x, y=values, fill=grouping_var)) +
geom_col(position=position_dodge(preserve = "single")) +
theme_classic()
当您使用position_dodge2()
时:
ggplot(df, aes(x=x, y=values, fill=grouping_var)) +
geom_col(position=position_dodge2(preserve = "single")) +
theme_classic()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.