簡體   English   中英

在ggplot2 barplot中組織堆疊的錯誤欄

[英]Organizing stacked errorbars in ggplot2 barplot

我正在使用條形圖中的概率數據。 該條形圖看起來不錯,但是當我嘗試添加錯誤欄時,它們沒有被組織或綁定到適當的列。 如果我不必手動執行此操作,那將是完美的。 (此示例僅顯示一個子集。)

您能幫我弄清楚如何在正確的位置添加錯誤欄嗎?

這是我的代碼:

data[complete.cases(data),] %>% 
  ggplot(aes(x = Var.name, y = Prob_mean, fill = Response)) + 
  geom_bar(stat = "identity", position = "fill") + 
  geom_errorbar(aes(ymin = lower, ymax = Prob_mean), stat = "identity", 
                position = position_dodge(width = 0.9), width = 0.3) 

圖的圖片:

position_dodge

這是我的數據:

> dput(data)
structure(list(Var.name = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 4L, 4L, 4L), .Label = c("Biomass", "Bacteria.total", 
"Gpos", "Gneg"), class = "factor"), Response = c("Decrease", 
"Increase", "Neutral", "Decrease", "Increase", "Neutral", "Decrease", 
"Increase", "Neutral", "Decrease", "Increase", "Neutral"), Prob_mean = c(0.267825247615756, 
0.589316466289715, 0.142858286094529, 0.272971629090396, 0.575649507723523, 
0.09076335727541, 0.392857191685997, 0.357142750965404, 0.250000057348599, 
0.499952457038418, 0.392844812870964, 0.107202730090619), Prob_n = c(56L, 
56L, 56L, 33L, 33L, 33L, 28L, 28L, 28L, 28L, 28L, 28L), Prob_se = c(0.023672823211199, 
0.0344812896356437, 0.0143539351411692, 0.0319407298087323, 0.0477306098555942, 
0.0300122568287155, 0.0553692531148468, 0.036801084975757, 0.0609410527844315, 
0.0252493129246497, 0.0320001555212106, 0.0344986116155648), 
    lower = c(0.244152424404557, 0.554835176654071, 0.12850435095336, 
    0.241030899281664, 0.527918897867928, 0.0607511004466945, 
    0.33748793857115, 0.320341665989647, 0.189059004564168, 0.474703144113768, 
    0.360844657349753, 0.0727041184750539), upper = c(0.291498070826955, 
    0.623797755925359, 0.157212221235698, 0.304912358899128, 
    0.623380117579117, 0.120775614104125, 0.448226444800843, 
    0.393943835941161, 0.310941110133031, 0.525201769963067, 
    0.424844968392174, 0.141701341706184)), .Names = c("Var.name", 
"Response", "Prob_mean", "Prob_n", "Prob_se", "lower", "upper"
), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"
))

編輯:

我想堆疊錯誤欄,但是當我使用“堆疊”時會發生這種情況: 堆疊
錯誤:堆棧未固定在軸上時定義不正確。 我想將較低的誤差線作為堆積位置,而不是躲避位置。

我的問題不同於如何使用geom_errorbar在堆疊條形圖中堆疊誤差條? ,因為它使用了“堆棧”功能,而我的錯誤欄是在使用“填充”功能時出現的問題。

我幾乎完全解決了這個問題。 但是,我必須返回數據以排除圖中未顯示的另一個響應選項。 這影響了我的錯誤欄,因為它們是正確的“堆棧”而不是“填充”(因為數據按比例縮放為“填充”選項)。 當我刪除該數據並再次運行分析時,誤差線非常完美。

這是我的解決方案:

data <- data %>% 
  mutate(vadj = ifelse(Response == "Neutral", 0, Prob_mean))

neutral.val = data %>% 
  filter(Response == "Neutral")

rep(neutral.val$Prob_mean, each=3)

incr.val = data %>% 
  filter(Response == "Increase")

rep(incr.val$Prob_mean, each=3)

data <- data %>% 
  mutate(Incr.val =rep(incr.val$Prob_mean, each=3), Neutral.val = rep(neutral.val$Prob_mean, each=3)) %>% 
  mutate(vadj = ifelse(Response == "Neutral", 0, 
                       ifelse(Response == "Increase", Neutral.val, Neutral.val+Incr.val)))

data%>% 
  ggplot(aes(x = Var.name, y = Prob_mean, fill = Response)) + 
  geom_bar(stat = "identity", position = "fill") + 
  geom_errorbar(aes(ymin = lower+vadj, ymax = Prob_mean+vadj), stat = "identity", #position = "stack",
                width = 0.3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM