我有以下 geom_bar 躲避圖,并認為 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"))
uj5u.com熱心網友回復:
OP,position_dodge2(preserve="single")代替position_dodge(preserve="single"). 出于某種原因,定心棒/列并不完全正確地作業position_dodge(),但它確實有position_dodge2()。請注意切換位置功能時獲得的間距略有不同,但總體上應該可以解決您的問題。
OP 問題的可重現示例
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))
基本情節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()

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/365228.html
上一篇:scale_fill_manual僅適用于圖例而不適用于條形圖
下一篇:如何在EF中使用強型別ID?
