【问题标题】:Convex hull on horizontal and vertical error bars水平和垂直误差条上的凸包
【发布时间】:2021-01-22 02:58:53
【问题描述】:

我有一个带有均值和水平和垂直误差线的 ggplot,我想添加一个包含所有误差线的凸包 - 像这样:

我尝试过使用ggpubr 中的stat_chull,但我不确定如何在每个点的误差条有xminxmaxyminymax 时指定美学。下面是我如何编码指定 stat_chull aes 作为 X 和 Y 表示的情节作为示例 - 我知道这是不正确的,但我认为我在正确的轨道上?

ggplot()+
  geom_point(data = df, aes(MeanX,MeanY)) +
  geom_errorbar(data = df, 
                mapping = aes(x = MeanX,
                              ymin = MeanY - SdY, 
                              ymax = MeanY + SdY), 
                width = 0, inherit.aes = FALSE)+
  geom_errorbarh(data = df, 
                 mapping = aes(y = MeanY,
                               xmin = MeanX - SdX,
                               xmax = MeanX + SdX),
                 height = 0, inherit.aes = FALSE)+
  stat_chull(data = df, aes(MeanX,MeanY))+
  theme_classic()

这给出了以下情节:

我也试过 geom_polygon 并得到了垃圾。

这是一个数据:

df<-structure(list(Source = structure(1:5, .Label = c("A", "B", "C", "D", 
                                                      "E"), class = "factor"), MeanX = c(-18.7066666666667, 
                                                                                                                                -15.8769230769231, -16.8620689655172, -15.72, -17.4333333333333
                                                      ), SdX = c(1.61072554509115, 0.409201849758959, 1.04811067886951, 
                                                                       0.74057035077327, 1.15902257671425), MeanY = c(9.93666666666667, 
                                                                                                                            14.3230769230769, 9.22758620689655, 11.1, 13.7333333333333), 
                   SdY = c(1.03005970142791, 0.539116085686704, 0.504990221704281, 
                                 0.757187779440037, 1.05039675043925)), row.names = c(NA, 
                                                                                      -5L), class = "data.frame")

非常感谢任何帮助!

【问题讨论】:

    标签: r ggplot2 convex-hull


    【解决方案1】:

    以下内容对您有用吗?

    library(dplyr)
    
    df %>%
      mutate(ymin = MeanY - SdY,
             ymax = MeanY + SdY,
             xmin = MeanX - SdX,
             xmax = MeanX + SdX) %>%
      ggplot(aes(x = MeanX, y = MeanY))+
      geom_point() +
      geom_errorbar(aes(ymin = ymin, ymax = ymax),
                    width = 0)+
      geom_errorbarh(aes(xmin = xmin, xmax = xmax),
                     height = 0)+
      stat_chull(data = . %>%
                   summarise(x = c(MeanX, MeanX, MeanX, xmin, xmax),
                             y = c(MeanY, ymin, ymax, MeanY, MeanY)),
                 aes(x = x, y = y),
                 geom = "polygon", colour = "black", fill = NA)+
      theme_classic()
    

    【讨论】:

    • 谢谢@Z.Lin。不是我预期的解决方法,但效果很好!