簡體   English   中英

如何在 ggplot/R 中按特定順序制作堆疊條形圖,同時保持特定的配色方案?

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

我有一個帶有百分比的堆積條形圖,解釋了有人留下差評的原因。 基於這個SO answer ,我將我的情節設計為一個單一的堆疊條形圖。 但是,我現在想根據選擇該原因的個人百分比來排序條形圖。 如果有平局,無論哪個原因先出現都可以,但配色方案需要保持一致(即原因 1 必須始終為黃色)。

我嘗試重新排序數據集( arrange(desc(percentage)) %>% ),但結果是一樣的。 我還看到其他答案說要訂購 x 軸,但我的 x 軸變量對於每個人來說都是相同的,這取決於我如何操縱情節。 任何幫助,將不勝感激!

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))

一種選擇是首先使用一個命名的顏色向量,它將顏色分配給類別。 要按百分比排序條形,您可以按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))

作為上述 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