堆叠 geom_bar 中的项目排序

Ordering of items within a stacked geom_bar

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

一个例子

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

从右到左,从 'Not at all' 到 'Very confident' 堆叠的条 运行。这些项目的顺序是正确的,从 'Multimedia' 到 'Personal',按照对每个项目说 'Not at all' 的人的比例排序。

我想要得到的是这张图表,其中的响应以另一种方式排列,与图例相同,即从左侧的 'Not at all' 到右侧的 'Very confident'。我不知道这个顺序是如何设置的,也不知道如何更改它。

我已通读 'similar questions',但看不到此特定查询的答案。建议,使用 ggplot,而不是 base 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,从上到下,响应的顺序是 'Very confident' 到 'Not at all',但默认图例是相反的顺序 'Not at all' 到 'Very confident'.

如果您传递 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<- 或重新组合因子很容易反转级别顺序。要保持相同级别的颜色,请将 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")