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. 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 (i.e., reason 1 must always be yellow).
I've tried reordering the dataset ( arrange(desc(percentage)) %>%
), but the results are the same. 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. 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))
CodePudding user response:
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
:
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))