這是一個可重現的樣本:
set.seed(42)
n <- 1000
dat <- data.frame(Participant=1:20,
Environment=rep(LETTERS[1:2], n/2),
Condition=rep(LETTERS[25:26], n/2),
Gate= sample(1:5, n, replace=TRUE),
Block = sample(1:2, n, replace=TRUE),
Sound=rep(LETTERS[3:4], n/2),
Correct=sample(0:1, n, replace=TRUE),
Target= sample(1:5, n, replace=TRUE)
)
當我繪制這個圖表時,兩組顯示為彼此上方/下方,而不是相互疊加,給我的平均值超過 100%。如何讓這些組并排或疊加顯示?
這是我的圖表的代碼:
dat %>%
group_by(Target, Sound) %>%
dplyr::summarize(Accuracy = mean(Correct),
se = sd(Correct)/sqrt(n())) %>%
ggplot(aes(x = Target, y = Accuracy, color = Sound, group = Sound))
geom_bar(stat = "identity")
scale_y_continuous(labels = scales::percent)
theme(axis.text.x = element_text(angle = 45))
scale_color_brewer(palette = "Set1")
我得到的圖表圖片:

uj5u.com熱心網友回復:
您可以使用引數位置調整 geom_bar 中組的位置:
geom_bar(stat = "identity", position = position_dodge())
我相信沒有必要group在ggplot(aes())
如果要更改所有條形而不是輪廓的顏色,請color更改fill
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/436075.html
