【问题标题】:Ordering of items within a stacked geom_bar在堆叠的 geom_bar 中对项目进行排序
【发布时间】:2017-04-15 23:00:51
【问题描述】:

出于对我来说似乎很好的原因,我想绘制一个堆积条形图,其中条形按特定的数据相关顺序排列。由于我不清楚的原因,它似乎不起作用。具体来说,虽然我可以很容易地以正确的顺序排列我的数据框的行,并使标识条形的名称列成为有序因子,因此按我想要的顺序获取条形图并没有列出数据框的列按照我想要的顺序。

一个例子

tab <- structure(list(Item = c("Personal", "Peripheral", "Communication", "Multimedia", "Office", "Social Media"), `Not at all` = c(3.205128, 18.709677, 5.844156, 31.578947, 20.666667, 25.827815), Somewhat = c(30.76923, 23.87097, 24.67532, 18.42105, 30, 16.55629), `Don't know` = c(0.6410256, 2.5806452, 1.9480519, 11.1842105, 2.6666667, 5.9602649), Confident = c(32.69231, 29.67742, 33.11688, 17.10526, 23.33333, 27.15232), `Very confident` = c(32.69231, 25.16129, 34.41558, 21.71053, 23.33333, 24.50331)), .Names = c("Item", "Not at all", "Somewhat", "Don't know", "Confident", "Very confident"), row.names = c(NA, -6L), class = "data.frame")

Title <- 'Plot title'
ResponseLevels <- c("Not at all", "Somewhat", "Don't know", "Confident", "Very confident") # Labels for bars

pal.1 <- brewer.pal(category, 'BrBG') # Colours

tab <- tab %>% arrange(.[,2]) # Sort by first columns of responses
tab$Item <- factor(tab$Item, levels = tab$Item[order(tab[,2])], ordered = TRUE) # Reorder factor levels

tab.m <- melt(tab, id = 'Item')
tab.m$col <- rep(pal.1, each = items) # Set colours

g <- ggplot(data = tab.m, aes(x = Item, y = value, fill = col)) + 
    geom_bar(position = "stack", stat = "identity", aes(group = variable)) +
    coord_flip() +
    scale_fill_identity("Percent", labels = ResponseLevels, 
                        breaks = pal.1, guide = "legend") +
    labs(title = Title, y = "", x = "") +
    theme(plot.title = element_text(size = 14, hjust = 0.5)) +
    theme(axis.text.y = element_text(size = 16,hjust = 0)) +
    theme(legend.position = "bottom")

g

堆叠的条形图从右到左排列,从“一点也不”到“非常自信”。这些项目按正确的顺序排列,从“多媒体”到“个人”,按说“一点也不”的人占每个项目的比例排序。

我想要得到的是这张图表,其中的响应以另一种方式排列,与图例相同,即从左侧的“一点也不”到右侧的“非常有信心”。我无法弄清楚这个顺序是如何设置的,也不知道如何更改它。

我已通读“类似问题”,但看不到此特定问题的答案。欢迎提出建议,使用 ggplot,而不是基本 R 图形。

好的,基于 allstaire 的有用且非常感谢的答案,我尝试以下方法

library(tidyverse)

tab <- structure(list(Item = c("Personal", "Peripheral", "Communication", "Multimedia", "Office", "Social Media"), `Not at all` = c(3.205128, 18.709677, 5.844156, 31.578947, 20.666667, 25.827815), Somewhat = c(30.76923, 23.87097, 24.67532, 18.42105, 30, 16.55629), `Don't know` = c(0.6410256, 2.5806452, 1.9480519, 11.1842105, 2.6666667, 5.9602649), Confident = c(32.69231, 29.67742, 33.11688, 17.10526, 23.33333, 27.15232), `Very confident` = c(32.69231, 25.16129, 34.41558, 21.71053, 23.33333, 24.50331)), .Names = c("Item", "Not at all", "Somewhat", "Don't know", "Confident", "Very confident"), row.names = c(NA, -6L), class = "data.frame")

tab <- tab %>% select(1,6,5,4,3,2,1) ## Re-order the columns of tab

tab.m <- tab %>% arrange(`Not at all`) %>%
mutate(Item = factor(Item, levels = Item[order(`Not at all`)])) %>% 
gather(variable, value, -Item, factor_key = TRUE)

ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
geom_col() +
coord_flip() +
scale_fill_brewer("Percent", type = 'cat', palette = 'BrBG', 
                  guide = guide_legend(reverse = TRUE)) +
labs(title = 'Plot title', y = NULL, x = NULL) +
theme(legend.position = "bottom")

这正是我想要的图表,所以我的紧迫问题解决了。

但是,如果我说,

ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
geom_col() +
coord_flip() +
scale_fill_brewer("Percent", type = 'cat', palette = 'BrBG', 
                  guide = guide_legend(reverse = FALSE)) +
labs(title = 'Plot title', y = NULL, x = NULL) +
theme(legend.position = "bottom")

我得到的图片是这样的

这里的图表主体是正确的,但图例的方向是错误的。

这解决了我的问题,但并不能完全回答我的问题。我从一个数据框开始,为了得到我想要的东西,我必须颠倒数据列的顺序,并颠倒指南图例。这显然有效,但它是反常的。

那么,堆积条形图如何决定以什么顺序呈现堆积的项目?这显然与它们在融化数据集中的顺序有关,但简单地改变顺序会使图例走向错误的方向。从上到下查看融化的数据集 tab.m,响应的顺序是“非常有信心”到“一点也不”,但默认图例是“一点也不”到“非常有信心”的相反顺序.

【问题讨论】:

    标签: r ggplot2 geom-bar stacked


    【解决方案1】:

    如果您传递guide_legend 而不仅仅是一个字符串,您可以将其reverse 参数设置为TRUE。简化一点,

    library(tidyverse)
    
    tab <- structure(list(Item = c("Personal", "Peripheral", "Communication", "Multimedia", "Office", "Social Media"), `Not at all` = c(3.205128, 18.709677, 5.844156, 31.578947, 20.666667, 25.827815), Somewhat = c(30.76923, 23.87097, 24.67532, 18.42105, 30, 16.55629), `Don't know` = c(0.6410256, 2.5806452, 1.9480519, 11.1842105, 2.6666667, 5.9602649), Confident = c(32.69231, 29.67742, 33.11688, 17.10526, 23.33333, 27.15232), `Very confident` = c(32.69231, 25.16129, 34.41558, 21.71053, 23.33333, 24.50331)), .Names = c("Item", "Not at all", "Somewhat", "Don't know", "Confident", "Very confident"), row.names = c(NA, -6L), class = "data.frame")
    
    tab.m <- tab %>% arrange(`Not at all`) %>%
        mutate(Item = factor(Item, levels = Item[order(`Not at all`)])) %>% 
        gather(variable, value, -Item, factor_key = TRUE)
    
    ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
        geom_col() +
        coord_flip() +
        scale_fill_brewer("Percent", palette = 'BrBG', 
                          guide = guide_legend(reverse = TRUE)) +
        labs(title = 'Plot title', y = NULL, x = NULL) +
        theme(legend.position = "bottom")
    


    对于编辑:

    条形顺序由因子水平顺序决定,在上面由于使用gather 创建因子而由列顺序决定,尽管coord_flip 使其不那么明显。不过,使用levels&lt;- 或通过重新组装因子很容易反转级别顺序。要使颜色保持相同的级别,请将direction = -1 传递给scale_fill_brewer 以颠倒它们的顺序。

    tab.m <- tab %>% arrange(`Not at all`) %>%
        mutate(Item = factor(Item, levels = Item[order(`Not at all`)])) %>% 
        gather(variable, value, -Item, factor_key = TRUE) %>% 
        mutate(variable = factor(variable, levels = rev(levels(variable)), ordered = TRUE))
    
    ggplot(data = tab.m, aes(x = Item, y = value, fill = variable)) + 
        geom_col() +
        coord_flip() +
        scale_fill_brewer("Percent", palette = 'BrBG', direction = -1,
                          guide = guide_legend(reverse = TRUE)) +
        labs(title = 'Plot title', y = NULL, x = NULL) +
        theme(legend.position = "bottom")
    

    【讨论】:

    • 这是一个很好的建议,感谢您提请我注意。它不能完全满足我的需要,因为图表仍然以错误的方式运行。在我的大部分工作中,反应的顺序都是自然的。在这种情况下,从“一点也不”到“非常有信心”。就像读者期望 x 轴右侧的数字更大一样,他们期望右侧的“非常自信”,而不是左侧。我现在将尝试将 ResponseLevels 以相反的顺序放置,以查看图形是否以正确的顺序出现。再次感谢!
    • 完美,我现在明白了。再次感谢您清晰的解释。
    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多