Reputation: 87
I have a data that look like this
structure(list(country = c("CHN", "CHN", "CHN", "JPN", "JPN",
"JPN", "KAZ", "KAZ", "KAZ", "KOR", "KOR", "KOR", "SYR", "SYR",
"SYR", "THA", "THA", "UZB", "UZB", "VIE", "VIE"), Final_Medal = structure(c(3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 2L,
1L, 2L, 3L, 2L), .Label = c("GOLD", "SILVER", "BRONZE"), class = "factor"),
n = c(17L, 28L, 15L, 15L, 9L, 18L, 2L, 1L, 2L, 2L, 10L, 4L,
1L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 1L)), row.names = c(NA, -21L
), groups = structure(list(country = c("CHN", "JPN", "KAZ", "KOR",
"SYR", "THA", "UZB", "VIE"), .rows = structure(list(1:3, 4:6,
7:9, 10:12, 13:15, 16:17, 18:19, 20:21), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -8L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
For the structur of the final medal, I have change it to factor and give it a new level.
medal_records$Final_Medal<- factor(medal_records$Final_Medal, levels= c("GOLD","SILVER","BRONZE"))
However when I plot it in ggplot the bar order are still the same in BRONZE, Silver and Gold, and I want Gold on the top followed by silver and bronze. Here's my code
mr <- ggplot(medal_records )+
geom_col(aes(x = n, y = country, fill = Final_Medal), position = "dodge", width = 0.7) +
scale_x_continuous(limits = c(0, 35)) +
scale_y_discrete(limits = rev) +
#scale_fill_manual(values = pal_mk[4:5]) +
labs(
title = "Medal Records",
subtitle = "Top 8 country with the most medals in internationl event"
) +
theme(
legend.position = c(0.875, 0.5)
Upvotes: 0
Views: 808
Reputation: 15123
By desc
,
ggplot(medal_records )+
geom_col(aes(x = n, y = country, group = desc(Final_Medal), fill = Final_Medal), position = "dodge", width = 0.7) +
scale_x_continuous(limits = c(0, 35)) +
scale_y_discrete(limits = rev) +
#coord_flip() +
#scale_fill_manual(values = pal_mk[4:5]) +
labs(
title = "Medal Records",
subtitle = "Top 8 country with the most medals in internationl event"
) +
theme(
legend.position = c(0.875, 0.5))
Upvotes: 2