【问题标题】:ggplot error bar positioningggplot误差条定位
【发布时间】:2017-10-02 12:19:31
【问题描述】:

我希望我的问题格式正确,并且已经广泛搜索类似的问题,所以如果这是重复的,我深表歉意

当我将误差条放在分组条形图上时,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))

将非常感谢任何见解

【问题讨论】:

    标签: r ggplot2 errorbar


    【解决方案1】:

    需要匹配errorbar geom与bars的分组方式:

    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))
    

    【讨论】:

      【解决方案2】:

      您的ggplot 调用中的数据仅在调用aes(fill = variable) 中按variable 字段分组在一起。因此,只有 geom_bar 知道该分组。

      有两种解决方案: 首先,您可以通过添加group = variable 将分组通知geom_errorbar,以便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))
      

      给出输出:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-26
        • 1970-01-01
        • 2021-08-10
        • 2022-08-12
        • 1970-01-01
        • 1970-01-01
        • 2017-11-03
        相关资源
        最近更新 更多