I'm a complete novice when it comes to this. I have created a stacked bar chart in R, but my y axis categories are in the wrong order. Please can someone advise on how to reorder them?
The code I using is:
scenarios_summary_diverging_right_order %>%
ggplot(aes(x = Scenario,
y = percent_answers,
fill = Opinion)) +
geom_col() +
geom_text(aes(label = percent_answers_label),
position = position_stack(vjust = 0.5),
color = "white",
fontface = "bold") +
coord_flip() +
scale_x_discrete() +
scale_fill_viridis_d(breaks = c("Strongly disagree", "Disagree", "Undecided","Agree", "Strongly agree")) +
labs(title = "To what extent do you agree that this is a display of aggressive behaviour?",
x = NULL,
fill = NULL) +
theme_minimal() +
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
panel.grid = element_blank(),
legend.position = "top")
This has produced a chart that I am happy with except for the order of categories on the y axis.
I tried this (found on Google) - but could be barking up the wrong tree!
scenarios_summary_diverging_right_order %>%
mutate(Scenario = fct_reorder(Scenario, Opinion)) %>%
ggplot( aes(x=Opinion, y=Scenario)) +
geom_bar(stat="identity", fill="#f68060", alpha=.6, width=.4) +
coord_flip() +
xlab("") +
theme_bw()
I looked through the suggested questions and answers but was struggling to understand where I might have wrong.
Example data:
structure(list(Scenario = c("Scenario 1", "Scenario 1", "Scenario
1",
"Scenario 1", "Scenario 1", "Scenario 2", "Scenario 2", "Scenario
2",
"Scenario 2", "Scenario 2"), Opinion = structure(c(2L, 5L, 1L,
4L, 3L, 2L, 5L, 1L, 4L, 3L), levels = c("Strongly agree", "Agree",
"Undecided", "Strongly disagree", "Disagree"), class = "factor"),
n_answers = c(25L, 98L, 1L, 20L, 107L, 54L, 63L, 4L, 21L,
105L), percent_answers = c(0.099601593625498, -0.390438247011952,
0.00398406374501992, -0.0796812749003984, 0.426294820717131,
0.218623481781377, -0.255060728744939, 0.0161943319838057,
-0.0850202429149798, 0.425101214574899), percent_answers_label =
c("10%",
"39%", "0%", "8%", "43%", "22%", "26%", "2%", "9%", "43%"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
Thank you in advance!
You have set levels for the Opinion variable in your initial dataframe as
c("Strongly agree", "Agree", "Undecided", "Strongly disagree", "Disagree")
Then you change that order in scale_fill_*
c("Strongly disagree", "Disagree", "Undecided", "Agree", "Strongly agree"))
In addition, for the Opinions variable, you are not explicitly calling the labels argument to force the mapping between the numeric levels and the character labels.
Maybe this example will get you a bit closer to your desired plot
scenarios_summary_diverging_right_order <- structure(list(
Scenario = c("Scenario 1", "Scenario 1", "Scenario 1",
"Scenario 1", "Scenario 1", "Scenario 2",
"Scenario 2", "Scenario 2", "Scenario 2",
"Scenario 2"),
Opinion = structure(c(2L, 5L, 1L,
4L, 3L, 2L, 5L,
1L, 4L, 3L),
labels = c("Disagree", "Strongly agree","Strongly disagree",
"Agree", "Undecided","Disagree", "Strongly agree",
"Strongly disagree", "Agree", "Undecided"),
levels = c("Strongly disagree", "Disagree", "Undecided", "Strongly agree", "Agree"),
class = "factor"),
n_answers = c(25L, 98L, 1L, 20L, 107L, 54L, 63L, 4L, 21L,
105L),
percent_answers = c(0.099601593625498, -0.390438247011952,
0.00398406374501992, -0.0796812749003984, 0.426294820717131,
0.218623481781377, -0.255060728744939, 0.0161943319838057,
-0.0850202429149798, 0.425101214574899),
percent_answers_label =
c("10%", "39%", "0%", "8%", "43%", "22%", "26%", "2%", "9%", "43%"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
scenarios_summary_diverging_right_order %>%
ggplot(aes(x = Scenario,
y = percent_answers,
fill = Opinion)) +
geom_col() +
geom_text(aes(label = paste0(percent_answers_label, '\n', str_wrap(Opinion, 7))),
position = position_stack(vjust = 0.5),
color = "white",
size = 3,
fontface = "bold") +
coord_flip() +
labs(title = "To what extent do you agree that this is a display of aggressive behaviour?",
x = NULL,
fill = NULL) +
theme_minimal() +
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
panel.grid = element_blank(),
legend.position = "none")