简体   繁体   English

如何在 ggplot/R 中按特定顺序制作堆叠条形图,同时保持特定的配色方案?

[英]How do I make stacked bar chart in specific order in ggplot/R while maintaining a specific color scheme?

I have a stacked bar chart with percentages explaining the reason someone left a bad review.我有一个带有百分比的堆积条形图,解释了有人留下差评的原因。 Based on this SO answer , I have rigged my plot to be a single, stacked bar chart.基于这个SO answer ,我将我的情节设计为一个单一的堆叠条形图。 However, I now want to order the bars based on the percentage of individuals who chosen that reason.但是,我现在想根据选择该原因的个人百分比来排序条形图。 If there's ties, it's okay whichever reason comes first, but the color scheme needs to stay consistent (ie, reason 1 must always be yellow).如果有平局,无论哪个原因先出现都可以,但配色方案需要保持一致(即原因 1 必须始终为黄色)。

I've tried reordering the dataset ( arrange(desc(percentage)) %>% ), but the results are the same.我尝试重新排序数据集( arrange(desc(percentage)) %>% ),但结果是一样的。 I've also seen other answers saying to order the x-axis, but my x axis variable is the same for everyone based on how I rigged the plot.我还看到其他答案说要订购 x 轴,但我的 x 轴变量对于每个人来说都是相同的,这取决于我如何操纵情节。 Any help would be appreciated!任何帮助,将不胜感激!

library(dplyr)
library(ggplot2)
library(scales)

#Test dataset
test_dataset <- tibble(reason = c("Reason 1", "Reason 2", "Reason 3", "Reason 4", "Reason 5"),
                       percentage = c(.10, .35, .25, .15, .15),
                       filler_variable = "filler_variable")

#specifying colors
colors <- c("red", "blue", "green", "orange", "yellow")

#Setting my variables in a set order to link them with their color
test_dataset$reason_factor <- factor(test_dataset$reason, levels= rev(c("Reason 1",
                                                                         "Reason 2",
                                                                         "Reason 3",
                                                                         "Reason 4",
                                                                        "Reason 5")))
#Making my plot
test_dataset %>%
  arrange(desc(percentage)) %>%
  ggplot(aes(x = filler_variable, y = percentage, fill = reason_factor)) + 
  geom_col(position="fill",width = 0.4) +
  coord_flip() +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0, 1)) +
  scale_fill_manual(values = colors, guide = guide_legend(reverse = TRUE, nrow=2, byrow=TRUE))

One option would be to first use a named vector of colors which fixes the assignment of colors to categories.一种选择是首先使用一个命名的颜色向量,它将颜色分配给类别。 To order your bars by percentages you could reorder your reason_factor by percentage :要按百分比排序条形,您可以按percentage reorder reason_factor

colors <- c("red", "blue", "green", "orange", "yellow")
names(colors) <- levels(test_dataset$reason_factor)

# Making my plot
ggplot(test_dataset, aes(y = filler_variable, x = percentage, fill = reorder(reason_factor, percentage))) +
  geom_col(position = "fill", width = 0.4) +
  scale_x_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0, 1)) +
  scale_fill_manual(values = colors, guide = guide_legend(reverse = TRUE, nrow = 2, byrow = TRUE))

As an extension to Stefan's answer above, here is how you can get the legend to be rearranged as well:作为上述 Stefan 答案的扩展,您可以通过以下方式重新排列图例:

#Run my original code to make dataset first

#Stefan's addition
colors <- c("red", "blue", "green", "orange", "yellow")
names(colors) <- levels(test_dataset$reason_factor)

#Makes a vector of the variables in the proper order
legend_order <- test_dataset %>%
  arrange(desc(percentage)) %>%
  dplyr::select(reason) %>%
  pull()

#Make the plot using Stefan's improvements, but now in scale_fill_manual (last line), add in a limits argument to set order of the key
test_dataset %>%
  arrange(desc(percentage)) %>%
  ggplot(aes(x = filler_variable, y = percentage, fill = reorder(reason_factor, percentage))) + 
  geom_col(position="fill",width = 0.4) +
  coord_flip() +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0, 1)) +
  scale_fill_manual(values = colors, guide = guide_legend(nrow=2, byrow=TRUE), limits = legend_order)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM