【问题标题】:geom_segment line vertical for multiple variable plot多变量绘图的 geom_segment 线垂直
【发布时间】:2021-06-07 10:18:57
【问题描述】:

下面的绘图代码使线段会聚在所有颜色的离散 x 变量的中心。怎么做才能使每个 x 变量对应颜色的线段垂直?

#~ Plot data
ggplot(data = data0, aes(x = v3, y = v4)) +
  geom_point(aes(colour = v2),
             size = 2,
             alpha = .25,
             position = position_jitterdodge(jitter.width = 0.15, seed = 1)
  ) +
  stat_summary(fun = mean, geom = "point", size = 5, aes(colour = v2), position = position_dodge(0.75)) +
  geom_segment(data = data1, aes(x = v3, xend = v3, y = v4, yend = grand_mean, colour = v2), 
               size = 1, position = position_dodge(width = 0.75)) +
  geom_hline(data = data1, aes(yintercept = grand_mean)) +
  facet_wrap(facets = vars(v1)) +
  theme_bw()

数据:

#~ Raw data
data0 <- data.frame(v1 = sample(c("foo", "bar"), 1000, replace = TRUE),
                    v2 = sample(letters[1:4], 1000 , replace = TRUE),
                    v3 = sample(1:4, 1000, replace = TRUE),
                    v4 = rnorm(1000))

#~ Summary data
data1 <- data0 %>%
  group_by(v1, v2, v3) %>%
  summarise(v4 = mean(v4))
data1$grand_mean <- mean(data0$v4)

【问题讨论】:

    标签: r ggplot2 geom-segment


    【解决方案1】:

    position_dodge() 无法处理 xendyend。请改用geom_line()geom_linerange()

    ggplot(data = data0, aes(x = v3, y = v4)) +
      geom_point(aes(colour = v2),
                 size = 2,
                 alpha = .25,
                 position = position_jitterdodge(jitter.width = 0.15, seed = 1)
      ) +
      stat_summary(fun = mean, geom = "point", size = 5, aes(colour = v2), position = position_dodge(0.75)) +
      geom_linerange(data = data1, aes(x = v3, ymin = v4, ymax = grand_mean, colour = v2), 
                   size = 1, position = position_dodge(width = 0.75)) +
      geom_hline(data = data1, aes(yintercept = grand_mean)) +
      facet_wrap(facets = vars(v1)) +
      theme_bw()
    

    【讨论】:

      【解决方案2】:

      更长的答案 - 我在其中手动计算每个组的 v3_revised(x 轴)位置。

      library(dplyr)
      library(ggplot2)
      
      #~ Raw data
      data0 <- data.frame(v1 = sample(c("foo", "bar"), 1000, replace = TRUE),
        v2 = sample(letters[1:4], 1000 , replace = TRUE),
        v3 = sample(1:4, 1000, replace = TRUE),
        v4 = rnorm(1000))
      
      v2_unique <- sort(unique(data0$v2))
      x_variation <- seq(from = -0.3, to = 0.3, length.out = length(v2_unique))
      names(x_variation) <- v2_unique
      #~ Summary data
      data0 <- data0 %>%
        mutate(v3_revised = v3 + x_variation[v2])
      data1 <- data0 %>%
        group_by(v1, v2, v3_revised) %>%
        summarise(v4 = mean(v4))
      #> `summarise()` has grouped output by 'v1', 'v2'. You can override using the `.groups` argument.
      data1$grand_mean <- mean(data0$v4)
      
      #~ Plot data
      ggplot(data = data0, aes(x = v3_revised, y = v4)) +
        geom_point(aes(colour = v2),
          size = 2,
          alpha = .25,
          position = position_jitterdodge(jitter.width = 0.15, seed = 1)) +
        stat_summary(fun = mean, geom = "point", size = 2, aes(colour = v2)) +
        geom_segment(data = data1, aes(x = v3_revised, xend = v3_revised,
          y = v4, yend = grand_mean,
          colour = v2), 
          size = 1) +
        geom_hline(data = data1, aes(yintercept = grand_mean)) +
        facet_wrap(facets = vars(v1)) +
        theme_bw()
      #> Warning: position_jitterdodge requires non-overlapping x intervals
      
      #> Warning: position_jitterdodge requires non-overlapping x intervals
      

      reprex package (v2.0.0) 于 2021-06-07 创建

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-25
        • 2014-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-22
        • 1970-01-01
        相关资源
        最近更新 更多