【问题标题】:ggplot strange ordering of stacked barggplot堆叠条的奇怪排序
【发布时间】:2017-11-07 11:33:43
【问题描述】:

我正在尝试创建一个像 here 这样的发散堆叠条,并且遇到了与此 SO question 类似的问题。我的方法略有不同,因为我通过一个数据集而不是两个数据集进行管理,而且我的颜色与我的数据无关。

表示如下:

library(tidyverse)
library(RColorBrewer)
x <- tribble(
  ~response, ~count,
  0,         -27,
  1,          -9,
  2,         -41,
  3,         -43,
  4,         -58,
  5,        -120,
  5,         120,
  6,         233,
  7,         379,
  8,         388,
  9,         145,
  10,         61
) %>% 
  mutate(response = factor(response))

ggplot(x, aes(x = 1, y = count, fill = response)) +
  geom_col() +
  scale_fill_brewer(palette = "RdBu") +
  coord_flip()

这给了我这样的图像:

问题在于堆叠数据在零的右侧的排序,在那里它们堆叠似乎是按降序排列的。任何关于如何解决此问题的想法将不胜感激(预期的排序将是 0-10,而不是 0-5,10-5)

【问题讨论】:

    标签: r ggplot2 dplyr


    【解决方案1】:

    一个艰难的!我玩过排序,似乎geom_bargeom_col 不喜欢将正值和负值以相同的顺序组合在一起。因此,我将数据框中的数据划分为正值和负值,为每个响应值生成颜色,并分别使用两个几何图形来表示正值和负值:

    library(tidyverse)
    library(RColorBrewer)
    x <- tribble(
      ~response, ~count,
      0,         -27,
      1,          -9,
      2,         -41,
      3,         -43,
      4,         -58,
      5,        -120,
      5,         120,
      6,         233,
      7,         379,
      8,         388,
      9,         145,
      10,         61
    ) %>% 
      # Get absolute values and add dummy to distuingish positive and negative values
      mutate(subzero = count < 0,
             count = abs(count))
    
    # Generate variable with colors from ColorBrewer for every response level (ugly but works)
    colors <- brewer.pal(length(unique(x$response)),"RdBu")
    x$colors <- NA
    for (i in 1:nrow(x)){
      x$colors[i] <- colors[x$response[i]+1]
    }
    
    
    ggplot() +
      geom_bar(data = x[x$subzero==T,], aes(x = "", y = -count, fill = reorder(colors, response)), position="stack", stat="identity") +
      geom_bar(data = x[x$subzero==F,], aes(x = "", y = count, fill = reorder(colors, -response)), position="stack", stat="identity") +
      geom_hline(yintercept = 0, color =c("black")) +
      scale_fill_identity("Response", labels = unique(x$response), breaks=unique(x$colors), guide="legend") +
      coord_flip() +
      labs(y="",x="") +
      theme(legend.position = "bottom", legend.direction = "horizontal") +
      scale_y_continuous(breaks=seq(-1400,1400,200), limits=c(-1400,1400))
    

    UPD:使 Y 比例平衡,使其看起来更清晰

    【讨论】:

      【解决方案2】:

      虽然不直观(对我而言),但使用:

      ggplot(x, aes(x = 1, y = order(count), fill = response)) +
        geom_col() +
        scale_fill_brewer(palette = "RdBu",direction=1) +
        coord_flip()
      

      它考虑了基于响应的排序(而不是 order(response))

      【讨论】:

      • 这是一个棘手的问题,因为最终这应该类似于从左到右排序 0:10 的李克特量表。 5 有 2 条记录的原因是因为我希望一半位于图表“中点”的任一侧。这种排序不太符合目的。
      • 啊是的,后来注意到顺序颠倒了(相对于图例)。此外,这些值表示有序值(不是实际计数),这使其成为次优答案:) 或者,您可以根据计数对变量进行排序,创建一个手动比例,包括 RColorBrewer::brewer.pal(11,"RdBu")(重复 5)并将其附加到您的 df.这可以用作scale_fill_manual 的输入
      【解决方案3】:

      你可以使用position_stack(reverse=TRUE):

      ggplot(x, aes(x = 1, y = count, fill = response)) +
        geom_col(position = position_stack(reverse=TRUE)) +
        scale_fill_brewer(palette = "RdBu") +
        coord_flip()
      

      【讨论】:

      • 仍然无法正常工作,因为红色最终出现在中间而不是蓝色。
      猜你喜欢
      • 2016-08-17
      • 2021-12-08
      • 1970-01-01
      • 2020-10-16
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2020-01-30
      相关资源
      最近更新 更多