代码之家  ›  专栏  ›  技术社区  ›  bob1

几何点和几何误差条不对齐

  •  2
  • bob1  · 技术社区  · 6 年前

    geom_errorbar 条形图不与平均点对齐(使用几何点),在某些情况下,水平条形图不与垂直条形图对齐 几何误差条

    我看了所有的手册页 ggplot geom_point , 几何误差条 position_jitter (闪避,抖动闪避)。我也从这里尝试了很多事情,比如改变 电话(例如。 How to make dodge in geom_bar agree with dodge in geom_errorbar, geom_point )

    以下是一组基本数据:

    df <- structure(list(
    Test = c("A", "B", "C", "D", "A", "C", "D"), 
    mean = c(1, 100.793684, 1, 1, 51.615601, 1, 2.456456), 
    sd = c(1, 2.045985, 1, 1, 4.790053, 1, 4.250668), 
    lower = c(2, 102.839669, 2, 2, 56.405654, 2, 6.707124), 
    upper = c(0, 98.747699, 0, 0, 46.825548, 0, -1.79421)), 
    row.names = c(NA, -7L), class = c("tbl_df", "tbl", "data.frame"))
    

    subplot <- ggplot(df, aes(x = Test, y = mean)) +
      geom_point(aes(x= Test, y = mean), 
                 position = position_jitter(width = 0.2, height = 0.2))+
      geom_errorbar(aes(ymin = lower, ymax = upper),
                    width = 0.1,
                    position = position_jitter(width = 0.2, height = 0.2)) 
    subplot
    

    这就是我得到的:

    Output of code above

    我怀疑这是我错过的最基本的东西。我已经使用了线图和其他散点图相同的代码,它一直很好,所以我不知道我做了什么。我也在两台不同的电脑上测试过它。

    非常感谢您的帮助。

    3 回复  |  直到 6 年前
        1
  •  2
  •   eipi10    6 年前

    第一,

    Test = c("A", "B", "C", "D", "A", "C", "D")
    mean = c(1, 100.793684, 1, 1, 51.615601, 1, 2.456456)
    sd = c(1, 2.045985, 1, 1, 4.790053, 1, 4.250668)
    lower = (mean+sd)
    upper = (mean-sd)
    range = 1:length(Test)
    
    df <- data.frame(Test,mean,sd,lower,upper,range)
    

    subplot <- ggplot(df, aes(x = Test, y = mean,group=range)) +
      geom_point(position = position_dodge(width = 0.2))+
      geom_errorbar(aes(ymin = lower, ymax = upper),
                    width = 0.1, position = position_dodge(width = 0.2)) 
    subplot
    

    image of plot

        2
  •  2
  •   bob1    6 年前

    我把这个数据集和问题发布到 ggplot Github page . 看来我确实错过了一件简单的事情——我需要为未来埋下种子 geom_ 为每个点调用一致的抖动。然而,似乎有一个问题 geom_errorbar

    经过进一步的调查(来自Github团队),似乎横杆是独立于线的抖动。有一个解决办法(截至2018年10月23日)来解决这个问题。同时使用 position_dodge geom_linerange

      ggplot(df, aes(x = Test, y = mean)) +
      geom_point(aes(x= Test, y = mean), 
                 position = position_jitter(width = 0.2, height = 0.2, seed = 123))+
      geom_linerange(aes(ymin = lower, ymax = upper),
                 position = position_jitter(width = 0.2, height = 0.2, seed = 123))
    

    谢谢大家的帮助。

        3
  •  1
  •   Jon Spring    6 年前

    看起来像 position_jitter 对errorbars的不同组件的应用是不同的。好像是个虫子。

    这里有一个解决方法,可以更直接地实现您的目标。添加一列(我称之为 version 这里)为了区分一个测试的多次运行, group 然后使用 position_dodge 以避免重叠。

    library(dplyr)
    df2 <- df %>% 
      group_by(Test) %>% 
      mutate(version = row_number()) %>% 
      ungroup()
    
    subplot <- ggplot(df2, aes(x = Test, y = mean, group = version)) +
      geom_point(position = position_dodge(width = 0.5))+
      geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2,
                position = position_dodge(width = 0.5)) 
    subplot
    

    enter image description here

    或者,我们可以使用facet\u grid,并根据测试的数量改变宽度,这将使误差条宽度保持一致。

    subplot <- ggplot(df2, aes(x = version, y = mean)) +
      geom_point(position = position_dodge(width = 0.5))+
      geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2,
                    position = position_dodge(width = 0.5)) +
      scale_x_continuous(breaks = NULL) +
      facet_grid(.~Test, space = "free_x", shrink = T, scales = "free_x")
    subplot
    

    enter image description here

    interaction(Test, version) 一个组合测试和版本的变量,使每次运行具有相同的宽度(我在使用时无法通过测试获得订单 interaction 接近。)

    df2 <- df %>% 
    group_by(Test) %>% 
      mutate(version = row_number()) %>% 
      mutate(label = paste(Test, version)) %>%
      ungroup()
    
    subplot <- ggplot(df2, aes(x = label, y = mean)) +
      geom_point(position = position_dodge(width = 0.5))+
      geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2,
                    position = position_dodge(width = 0.5))
    subplot
    

    enter image description here