Я признаю, что есть много похожих вопросов, и я проверил их, но все еще не могу решить мою проблему.
Попытка создать столбчатую диаграмму с накоплением в ggplot, но не может правильно упорядочить столбцы.
Мои данные выглядят так
structure(list(`Aimag/Capital` = c("Arkhangai", "Arkhangai",
"Bayan-Ulgii", "Bayankhongor", "Bayankhongor", "Bulgan", "Darkhan-Uul",
"Darkhan-Uul", "Dornod", "Dornod", "Dornogobi", "Dornogobi",
"Dundgobi", "Gobi-Altai", "Gobi-Altai", "Gobisumber", "Khentii",
"Khentii", "Khovd", "Khovd", "Khuvsgul", "Orkhon", "Selenge",
"Selenge", "Sukhbaatar", "Tuv", "Tuv", "Ulaanbaatar", "Ulaanbaatar",
"Umnugobi", "Umnugobi", "Uvs", "Uvs", "Uvurkhangai", "Uvurkhangai",
"Zavkhan"), Type = c("Actual usage of water m3 /Ground water/",
"Actual usage of water m3 /Surface water/", "Actual usage of water m3
/Ground water/",
"Actual usage of water m3 /Ground water/", "Actual usage of water m3
/Surface water/",
"Actual usage of water m3 /Surface water/", "Actual usage of water m3
/Ground water/",
"Actual usage of water m3 /Surface water/", "Actual usage of water m3
/Ground water/",
"Actual usage of water m3 /Surface water/", "Actual usage of water m3
/Ground water/",
"Actual usage of water m3 /Surface water/", "Actual usage of water m3
/Ground water/",
"Actual usage of water m3 /Ground water/", "Actual usage of water m3
/Surface water/",
"Actual usage of water m3 /Ground water/", "Actual usage of water m3 /Ground
water/",
"Actual usage of water m3 /Surface water/", "Actual usage of water m3
/Ground water/",
"Actual usage of water m3 /Surface water/", "Actual usage of water m3
/Surface water/",
"Actual usage of water m3 /Ground water/", "Actual usage of water m3 /Ground
water/",
"Actual usage of water m3 /Surface water/", "Actual usage of water m3
/Ground water/",
"Actual usage of water m3 /Ground water/", "Actual usage of water m3
/Surface water/",
"Actual usage of water m3 /Ground water/", "Actual usage of water m3
/Surface water/",
"Actual usage of water m3 /Ground water/", "Actual usage of water m3
/Surface water/",
"Actual usage of water m3 /Ground water/", "Actual usage of water m3
/Surface water/",
"Actual usage of water m3 /Ground water/", "Actual usage of water m3
/Surface water/",
"Actual usage of water m3 /Ground water/"), sumAmount = c(101278.1,
272246.7, 7528, 50421.54, 12550.4, 882605, 669312.5, 137418.5,
3587701.32, 244252.09, 266421.5, 1856, 116723, 1453, 277, 3515190,
744539.2, 9749.4, 59142, 9520, 1000, 21324857, 167905, 1077338.19,
772753.76, 572085.5, 5035012.18, 5645963.45, 68067, 22230322,
68351, 2564, 2238, 15950, 180928, 299912)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -36L), spec =
structure(list(
cols = list(`Company's registration number` = structure(list(), class =
c("collector_double",
"collector")), `Company name` = structure(list(), class =
c("collector_character",
"collector")), `Aimag/Capital` = structure(list(), class =
c("collector_character",
"collector")), `Soum/ District` = structure(list(), class =
c("collector_character",
"collector")), Type = structure(list(), class = c("collector_character",
"collector")), Amount = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"), groups = structure(list(
`Aimag/Capital` = c("Arkhangai", "Bayan-Ulgii", "Bayankhongor",
"Bulgan", "Darkhan-Uul", "Dornod", "Dornogobi", "Dundgobi",
"Gobi-Altai", "Gobisumber", "Khentii", "Khovd", "Khuvsgul",
"Orkhon", "Selenge", "Sukhbaatar", "Tuv", "Ulaanbaatar",
"Umnugobi", "Uvs", "Uvurkhangai", "Zavkhan"), .rows = list(
1:2, 3L, 4:5, 6L, 7:8, 9:10, 11:12, 13L, 14:15, 16L,
17:18, 19:20, 21L, 22L, 23:24, 25L, 26:27, 28:29, 30:31,
32:33, 34:35, 36L)), row.names = c(NA, -22L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE))
Мой код выглядит так
ggplot(eiti_water_stacked_aimag, aes(reorder(`Aimag/Capital`, sumAmount),
sumAmount, fill = Type)) +
geom_bar(stat = "identity") +
coord_flip() +
ylim(0, 30000000) +
theme_tufte() +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_blank()) +
theme(legend.position = "bottom") +
theme(legend.title = element_blank()) +
scale_fill_manual(values = c("#A6CEE3","#1F78B4"),
labels = c("Ground water ",
"Surface water"))
Iобычно используют функцию reorder
, но теперь этот порядок довольно странный.Любые идеи, как это исправить?Я пробовал forcats, но это тоже не сработало.