ggplot错误栏定位

时间:2017-10-02 12:19:31

标签: r ggplot2 errorbar

我希望我已经正确地格式化了这个问题并且搜索了类似的问题,所以如果这是重复的话我会道歉

当我在分组条形图上放置错误条时,ggplot只是将错误条放在分组条之间,如下图中rplot所示,但是我希望错误条位于其尊重条的中间

rplot

数据看起来像这样

print(dfm)
  REEF           variable    value Errortype     Error
1 Reef 1 Machine.Percentage 23.35068        ME 0.1341473
2 Reef 2 Machine.Percentage 23.85531        ME 0.4876110
3 Reef 3 Machine.Percentage 18.36640        ME 0.6022585
4 Reef 4 Machine.Percentage 16.98787        ME 0.5596818
5 Reef 1   Human.Percentage 21.12382        HE 0.1620290
6 Reef 2   Human.Percentage 28.22039        HE 0.1732592
7 Reef 3   Human.Percentage 18.14550        HE 0.8022002
8 Reef 4   Human.Percentage 15.50208        HE 0.4999109

否则相同的数据,但如果是优选的话,使用dput

> dput(dfm)
structure(list(X = 1:8, REEF = structure(c(1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L), .Label = c("Reef 1", "Reef 2", "Reef 3", "Reef 4"
), class = "factor"), variable = structure(c(2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L), .Label = c("Human.Percentage", "Machine.Percentage"
), class = "factor"), value = c(23.35068462, 23.85531136, 18.36640212, 
16.98786965, 21.12382394, 28.22039072, 18.14550265, 15.50208154
), Errortype = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("HE", 
"ME"), class = "factor"), Error = c(0.134147251, 0.487611042, 
0.602258513, 0.559681767, 0.162029047, 0.173259241, 0.802200189, 
0.499910856)), .Names = c("X", "REEF", "variable", "value", "Errortype", 
"Error"), class = "data.frame", row.names = c(NA, -8L))

这是代码

 ggplot (data = dfm, aes(x = REEF, y = value))+ 
        geom_bar(aes(fill = variable),stat = "identity",position = "dodge", width = 0.9)+
        theme_bw()+scale_fill_brewer(palette="Set1")+
        theme(axis.text.x = element_text(angle = 45, hjust = 1))+
        ylab("% Hard Coral")+xlab("Reef Name")+theme(legend.title=element_blank())+
        ggtitle("Hard Coral group_KER 1")+
        geom_errorbar(aes(ymin=value-Error, ymax=value+Error),
                      size=.5,  
                      width=.2,
                      position=position_dodge(0.9))

对任何见解都会非常感激

2 个答案:

答案 0 :(得分:0)

需要匹配错误栏geom与条形图的分组方式:

ggplot (data = dfm, aes(x = REEF, y = value))+ 
        geom_bar(aes(fill = variable),stat = "identity",position = "dodge", width = 0.9)+
        theme_bw()+scale_fill_brewer(palette="Set1")+
        theme(axis.text.x = element_text(angle = 45, hjust = 1))+
        ylab("% Hard Coral")+xlab("Reef Name")+theme(legend.title=element_blank())+
        ggtitle("Hard Coral group_KER 1")+
        geom_errorbar(aes(ymin=value-Error, ymax=value+Error, group = variable),
                      size=.5,
                      width=.2,
                      position=position_dodge(0.9))

答案 1 :(得分:0)

ggplot来电中的数据仅在variable来电中由aes(fill = variable)字段组合在一起。因此,只有geom_bar知道该分组。

有两种解决方案: 首先,您可以通过添加geom_errorbar来通知分组的group = variable,以便aes来电变为aes(ymin = value-Error, ymax = value+Error, fill = variable)

或者,您可以将fill = variablegeom_bar美学转移到ggplot美学,以便分组可用于所有后续功能:aes(x = REEF, y = value, fill = variable)

顺便说一句,将geom_barstat = "identity"一起使用可以而且应该通过使用包含它的geom_col来实现。因此,您可以使用以下命令获取所需的输出:

ggplot (data = dfm, aes(x = REEF, y = value, fill = variable))+ 
  geom_col(position = "dodge", width = 0.9)+
  theme_bw()+scale_fill_brewer(palette="Set1")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  ylab("% Hard Coral")+xlab("Reef Name")+theme(legend.title=element_blank())+
  ggtitle("Hard Coral group_KER 1")+
  geom_errorbar(aes(ymin=value-Error, ymax=value+Error),
                size=.5,  
                width=.2,
                position=position_dodge(0.9))

给出输出: plot with corrected error bars