我想,对于我来说看起来很好的原因,可以绘制堆积的条形图,条形图以特定的数据相关顺序排列。由于我不明白的原因,它似乎不起作用。具体来说,虽然我可以很容易地按照正确的顺序排列我的数据框的行,并使标识栏的名称列成为有序因子,因此按照我所需的顺序获取条形图,但图形没有列出数据框的列按照我的愿望。堆叠的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 all',到右边的'Very confidence'。我无法弄清楚这个顺序是如何设置的,也不知道如何改变它。
我已经读过'类似的问题',但可以看不到这个特定查询的答案。建议,使用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,响应按照'非常自信'到'完全不'的顺序,但默认图例是相反顺序'完全不','非常自信' 。
这是一个伟大的建议,并感谢你为它画上了我的注意。它并没有完成我所需要的,因为图表仍然运行错误。在我的大部分工作中,都有自然的排序和回应。在这种情况下,从“完全不”到“非常有信心”。正如读者在x轴上期望更大的数字,所以他们期望在右边而非左边的'非常自信'。现在我将尝试以相反的顺序放置ResponseLevels,以查看图形是否以正确的顺序排列。再次感谢! – astaines
完美,我现在明白了。再次感谢您的清晰解释。 – astaines