R 如何在堆叠条形图中反转几何误差条的位置?

R 如何在堆叠条形图中反转几何误差条的位置?,r,ggplot2,R,Ggplot2,我正在创建一个简单的堆叠geom_条,包含两个组和geom_errorbar。我已经颠倒了这两组的顺序,但是错误条的顺序将不会跟随 我尝试通过改变组的顺序作为因素: -填充=用于猫::fct_版本(级别) 以及更改数据帧中组的顺序: -排列(df、desc(标高)) 对于每一次尝试,绘图中的条形图都会成功地更改其顺序,但错误条形图永远不会跟随。。。。我想更改顺序,使具有最高值(深蓝色)的组位于底部,而具有最低值(浅蓝色)的组位于顶部。这两个数字说明了我对结果类型的看法。(该问题与误差条的位置有关

我正在创建一个简单的堆叠geom_条,包含两个组和geom_errorbar。我已经颠倒了这两组的顺序,但是错误条的顺序将不会跟随

我尝试通过改变组的顺序作为因素: -填充=用于猫::fct_版本(级别) 以及更改数据帧中组的顺序: -排列(df、desc(标高))

对于每一次尝试,绘图中的条形图都会成功地更改其顺序,但错误条形图永远不会跟随。。。。我想更改顺序,使具有最高值(深蓝色)的组位于底部,而具有最低值(浅蓝色)的组位于顶部。这两个数字说明了我对结果类型的看法。(该问题与误差条的位置有关,而与值无关)

我想要的是与此相反的:

为什么错误条的顺序不遵循错误条的顺序

以下是数据帧的子集:

结构(列表)(站名=结构(c(1L、2L、3L、4L、5L、6L、7L、, 8L、9L、10L、11L、12L、13L、14L、15L、16L、17L、18L、19L、20L、1L、, 2L、3L、4L、5L、6L、7L、8L、9L、10L、11L、12L、13L、14L、15L、16L、, 17L、18L、19L、20L、1L、2L、3L、4L、5L、6L、7L、8L、9L、10L、11L、, 12L,13L,14L,15L,16L,17L,18L,19L,20L),标签=c(“a”,“b”, “c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”、“k”、“l”、“m”、“n”、“o”、“p”, “q”、“r”、“s”、“t”,class=“factor”), 平均气温=c(3.525,3.025,1.45,2.975,1.9425,3.06667,1.68,, 1.9, 2.575, 4, 4.7, 5.566666667, 2.4, 3.05, 2.4, 5.325, 2.825, 1.825,1.2075,1.165,0.1,0.025,0.05,1.11e-16,0.055, 0, 0.0525, 0, 0.05, 0.05, 0.125, 0.266666667, 0.075, -0.125, 0.125, 0.075, 0.15, 0.025, 0.0725, -0.0075, 3.425, 3, 1.4, 2.975, 1.8875, 3.066666667, 1.6275, 1.9, 2.525, 3.95, 4.575, 5.3, 2.325, 3.175, 2.275, 5.25, 2.675, 1.8, 1.135, 1.1725 ),sd=c(0.763216876,0.170782513,0.2081666,0.457347424, 0.789023658, 0.776745347, 1.239462249, 0.816496581, 0.287228132, 1.699019325, 2.24053565, 1.77857621, 0.496655481, 0.873689495, 0.081649658, 0.994568583, 1.129527925, 0.713559154, 0.344806709, 0.246779254, 0.163299316, 0.05, 0.1, 0.141421356, 0.052599113, 0.1, 0.098446263, 0, 0.057735027, 0.057735027, 0.125830574, 0.152752523, 0.05, 0.206155281, 0.189296945, 0.05, 0.191485422, 0.125830574, 0.037749172, 0.069940451, 0.62915287, 0.141421356, 0.21602469, 0.49244289, 0.755397246, 0.757187779, 1.14441761, 0.816496581, 0.320156212, 1.755942292, 2.136000936, 1.637070554, 0.518812747, 0.81394103, 0.206155281, 1.034408043, 1.209338662, 0.594418483,0.318904374,0.278732249),分形=结构(c(3L, 3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L, 3L,3L,3L,3L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L, 2L,2L,2L,2L,2L,2L,2L,2L,2L,1L,1L,1L,1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L),标签=c(“单据”, “POC”、“TOC”)、class=“factor”)、class=“data.frame”、row.names=c(NA、, -(60升)


这里是解决方案,但堆叠解决方案确实提供了非常有意义的表示,因为错误条彼此重叠。我建议使用闪避图来更清晰地表示数据

要正确创建堆叠图,需要将“frac”列和因子级别按所需的打印顺序进行排序。
有关详细信息,请参见代码中的注释

df<-structure(list(Station.name = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L), .Label = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t"), class = "factor"), Ave = c(3.525, 3.025, 1.45, 2.975, 1.9425, 3.066666667, 1.68, 1.9, 2.575, 4, 4.7, 5.566666667, 2.4, 3.05, 2.4, 5.325, 2.825, 1.825, 1.2075, 1.165, 0.1, 0.025, 0.05, 1.11e-16, 0.055, 0, 0.0525, 0, 0.05, 0.05, 0.125, 0.266666667, 0.075, -0.125, 0.125, 0.075, 0.15, 0.025, 0.0725, -0.0075, 3.425, 3, 1.4, 2.975, 1.8875, 3.066666667, 1.6275, 1.9, 2.525, 3.95, 4.575, 5.3, 2.325, 3.175, 2.275, 5.25, 2.675, 1.8, 1.135, 1.1725 ), sd = c(0.763216876, 0.170782513, 0.2081666, 0.457347424, 0.789023658, 0.776745347, 1.239462249, 0.816496581, 0.287228132, 1.699019325, 2.24053565, 1.77857621, 0.496655481, 0.873689495, 0.081649658, 0.994568583, 1.129527925, 0.713559154, 0.344806709, 0.246779254, 0.163299316, 0.05, 0.1, 0.141421356, 0.052599113, 0.1, 0.098446263, 0, 0.057735027, 0.057735027, 0.125830574, 0.152752523, 0.05, 0.206155281, 0.189296945, 0.05, 0.191485422, 0.125830574, 0.037749172, 0.069940451, 0.62915287, 0.141421356, 0.21602469, 0.49244289, 0.755397246, 0.757187779, 1.14441761, 0.816496581, 0.320156212, 1.755942292, 2.136000936, 1.637070554, 0.518812747, 0.81394103, 0.206155281, 1.034408043, 1.209338662, 0.594418483, 0.318904374, 0.278732249), frac = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("DOC", "POC", "TOC"), class = "factor")), class = "data.frame", row.names = c(NA, -60L))

library(ggplot2)
library(dplyr)
library(forcats)

#dodge plot
ggplot(subset(df, frac %in% c("DOC", "POC")), 
       aes(x=Station.name, y=Ave, fill=frac))+
  geom_col(width=0.6, position = "dodge")+
  scale_fill_manual(values = c("lightskyblue", "dodgerblue2"))+
  geom_errorbar(aes(x=Station.name, ymin=Ave-sd, ymax=Ave+sd),
                width=.2,size=0.2, position = position_dodge(0.5))

df对于正在打印的错误条(Ave sd)&(Ave+sd),仅仅因为小条位于顶部,并不意味着它们的值发生了变化,因此错误条的位置不会随着打印顺序的变化而变化。为了获得解决方案,请编辑问题并提供样本数据供人们测试。感谢您的评论@DaveT我已尝试包含我的数据框架的子集,这是正确的方法吗?我不确定我是否理解你关于编辑我的问题(?)的评论。
df<-structure(list(Station.name = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L), .Label = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t"), class = "factor"), Ave = c(3.525, 3.025, 1.45, 2.975, 1.9425, 3.066666667, 1.68, 1.9, 2.575, 4, 4.7, 5.566666667, 2.4, 3.05, 2.4, 5.325, 2.825, 1.825, 1.2075, 1.165, 0.1, 0.025, 0.05, 1.11e-16, 0.055, 0, 0.0525, 0, 0.05, 0.05, 0.125, 0.266666667, 0.075, -0.125, 0.125, 0.075, 0.15, 0.025, 0.0725, -0.0075, 3.425, 3, 1.4, 2.975, 1.8875, 3.066666667, 1.6275, 1.9, 2.525, 3.95, 4.575, 5.3, 2.325, 3.175, 2.275, 5.25, 2.675, 1.8, 1.135, 1.1725 ), sd = c(0.763216876, 0.170782513, 0.2081666, 0.457347424, 0.789023658, 0.776745347, 1.239462249, 0.816496581, 0.287228132, 1.699019325, 2.24053565, 1.77857621, 0.496655481, 0.873689495, 0.081649658, 0.994568583, 1.129527925, 0.713559154, 0.344806709, 0.246779254, 0.163299316, 0.05, 0.1, 0.141421356, 0.052599113, 0.1, 0.098446263, 0, 0.057735027, 0.057735027, 0.125830574, 0.152752523, 0.05, 0.206155281, 0.189296945, 0.05, 0.191485422, 0.125830574, 0.037749172, 0.069940451, 0.62915287, 0.141421356, 0.21602469, 0.49244289, 0.755397246, 0.757187779, 1.14441761, 0.816496581, 0.320156212, 1.755942292, 2.136000936, 1.637070554, 0.518812747, 0.81394103, 0.206155281, 1.034408043, 1.209338662, 0.594418483, 0.318904374, 0.278732249), frac = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("DOC", "POC", "TOC"), class = "factor")), class = "data.frame", row.names = c(NA, -60L))

library(ggplot2)
library(dplyr)
library(forcats)

#dodge plot
ggplot(subset(df, frac %in% c("DOC", "POC")), 
       aes(x=Station.name, y=Ave, fill=frac))+
  geom_col(width=0.6, position = "dodge")+
  scale_fill_manual(values = c("lightskyblue", "dodgerblue2"))+
  geom_errorbar(aes(x=Station.name, ymin=Ave-sd, ymax=Ave+sd),
                width=.2,size=0.2, position = position_dodge(0.5))
#to create the stacked chart with the error bars in the proper location
#reverse the order of the factor and resort the data frame to match the new factor order
df$frac<-fct_rev(df$frac)
df<-df[rev(order(df$frac)),]

#calculate the limits based on the subset
df2<-df %>% filter(frac=="DOC" | frac=="POC") %>% 
  group_by(Station.name) %>% 
  mutate(ymin=cumsum(Ave)-sd, ymax=cumsum(Ave)+sd)

#plot
ggplot(df2, aes(x=Station.name, y=Ave, fill=frac))+
  geom_col(width=0.6)+
  scale_fill_manual(values = c("lightskyblue", "dodgerblue2"))+
  geom_errorbar(aes(x=Station.name, ymin=ymin, ymax=ymax),
                width=.2,size=0.2)