我想繪制一個帶有雙 Y 的圖表,兩者都是帶有 ggplot2 的條形圖,并且兩個條形都有自己的錯誤欄和標簽,以下代碼失敗。條形重疊,標簽無法顯示。
非常感謝。

df<- structure(list(dose = structure(1:3, .Label = c("0.5", "1", "2"
), class = "factor"), mean1 = c(13.23, 22.7, 26.06), sd1 = c(0.1,
0.2, 0.3), label = c("a", "b", "c"), mean2 = c(7.98, 16.77, 26.14
), sd2 = c(0.01, 0.2, 0.3), label2 = c("a", "b", "c")), row.names = c(NA,
-3L), class = "data.frame")
ggplot(df,aes(x = dose, fill = dose))
geom_bar(aes(y = mean1), position = 'dodge', stat="identity", width=.4)
geom_bar(aes(y = mean2/5), position = 'dodge', stat="identity", width=.4)
scale_y_continuous(sec.axis = sec_axis(~. *5, name = "mean2"))
geom_errorbar(aes(ymin = mean1, ymax = mean1 sd1), width=.07,
position=position_dodge(0.4))
geom_errorbar(aes(ymin = mean2, ymax = mean2 sd2), width=.07,
position=position_dodge(0.4))
geom_text(aes(y =mean1 sd1, label = label1),vjust = -0.5, position=position_dodge(0.4))
geom_text(aes(y =mean2 sd2, label = label2,),vjust = -0.5, position=position_dodge(0.4))
uj5u.com熱心網友回復:
這是你想要的?您只需要重組資料,以便可以按組躲避。
bind_rows(
df |>
select(dose, label, mean = mean1, sd = sd1) |>
mutate(group = 1),
df |>
select(dose, label, mean = mean2, sd = sd2) |>
mutate(group = 2,
mean = mean/5)
) |>
ggplot(aes(x = dose, fill = dose, group = group))
geom_bar(aes(y = mean), position = 'dodge', stat="identity", width=.4)
geom_errorbar(aes(ymin = mean, ymax = mean sd), width=.07,
position=position_dodge(0.4))
geom_text(aes(y =mean sd, label = label),vjust = -0.5, position=position_dodge(0.4))
scale_y_continuous(sec.axis = sec_axis(~. *5, name = "mean2"))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427629.html
