r - 在ggplot的条形图中重新排序x轴
问题描述
我想绘制一个条形图并按值对 x 轴进行排序。我知道我之前设法做到了,但我可能在某处删除了该代码。我已经坚持了一段时间了,所以我现在问你。
由于我的数据框包含超过 24,000 行,这里是示例数据。我希望重新创建它就足够了。
structure(list(country = c("Palestine", "Iraq", "Algeria", "Libya",
"Lebanon", "Jordan", "Egypt", "Palestine", "Libya", "Egypt",
"Libya", "Jordan", "Yemen", "Jordan", "Tunesia", "Iraq", "Lebanon",
"Lebanon", "Morocco", "Algeria", "Jordan", "Egypt", "Kuwait",
"Morocco", "Tunesia", "Palestine", "Yemen", "Lebanon", "Sudan",
"Lebanon", "Libya", "Palestine", "Yemen", "Tunesia", "Sudan",
"Yemen", "Morocco", "Jordan", "Palestine", "Palestine", "Libya",
"Palestine", "Libya", "Jordan", "Jordan", "Lebanon", "Iraq",
"Algeria", "Yemen", "Tunesia", "Lebanon", "Libya", "Yemen", "Egypt",
"Yemen", "Libya", "Palestine", "Egypt", "Tunesia", "Sudan", "Tunesia",
"Egypt", "Lebanon", "Iraq", "Kuwait", "Libya", "Tunesia", "Algeria",
"Morocco", "Egypt", "Tunesia", "Morocco", "Palestine", "Kuwait",
"Morocco", "Kuwait", "Morocco", "Palestine", "Morocco", "Lebanon",
"Iraq", "Egypt", "Morocco", "Algeria", "Jordan", "Sudan", "Sudan",
"Algeria", "Sudan", "Egypt", "Palestine", "Jordan", "Sudan",
"Iraq", "Egypt", "Tunesia", "Sudan", "Yemen", "Lebanon", "Iraq"
), female_head_gov = structure(c(3L, 3L, 4L, 3L, 2L, 2L, 4L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 1L, 3L, 2L, 3L, 3L, 1L, 1L, 3L,
3L, 4L, 2L, 4L, 2L, 1L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 5L, 2L, 2L,
4L, 2L, 4L, 2L, 4L, 3L, 2L, 1L, 4L, 3L, 2L, 2L, 2L, 2L, 1L, 3L,
2L, 2L, 2L, 2L, 3L, 5L, 1L, 1L, 1L, 2L, 2L, 4L, 2L, 2L, 3L, 2L,
4L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 3L, 2L, 4L, 1L, 2L,
3L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 3L, 3L, 1L, 1L), .Label = c("I strongly agree",
"I agree", "I disagree", "I strongly disagree", "Don't know"), class = "factor")), class = "data.frame", row.names = c(NA,
-100L))
我使用 forcats::fct_reorder 尝试了以下代码,但它没有给我想要的结果:
dataset %>%
group_by(country) %>%
filter(!is.na(female_head_gov), female_head_gov != "Don't know") %>%
mutate(female_head_gov = fct_collapse(female_head_gov,
`Agree/strongly agree` = c("I strongly agree", "I agree"),
`Disagree/strongly disagree` = c("I disagree", "I strongly disagree"))
) %>%
count(female_head_gov) %>%
mutate(prop = n / sum(n)) %>%
ggplot(aes(fct_reorder(country, prop), prop, fill = female_head_gov)) +
geom_col(position = "fill")
我也试过这个而不计算比例,但是我没有变量来订购国家变量。
dataset %>%
group_by(country) %>%
filter(!is.na(female_head_gov), female_head_gov != "Don't know") %>%
mutate(female_head_gov = fct_collapse(female_head_gov,
`Agree/strongly agree` = c("I strongly agree", "I agree"),
`Disagree/strongly disagree` = c("I disagree", "I strongly disagree"))
) %>%
ggplot(aes(country, fill = female_head_gov)) +
geom_bar(position = "fill")
任何想法我做错了什么?如果您有任何提示如何更有效地创建这样的条形图,请告诉我。:)
解决方案
我想这是因为每个国家/地区都有两个prop
. 如果您指定它的第一个值prop
按预期工作:
plot_df <- dataset %>%
group_by(country) %>%
filter(!is.na(female_head_gov), female_head_gov != "Don't know") %>%
mutate(female_head_gov = fct_collapse(female_head_gov,
`Agree/strongly agree` = c("I strongly agree",
"I agree"),
`Disagree/strongly disagree` = c("I disagree",
"I strongly disagree"))
) %>%
count(female_head_gov) %>%
mutate(prop = n / sum(n))
plot_df$country <- fct_reorder(plot_df$country, plot_df$prop, function(x) -x[1])
ggplot(plot_df, aes(country, prop, fill = female_head_gov)) +
geom_col(position = "fill")
推荐阅读
- azure-devops - 在新的拉取请求上自动创建工作项
- python - 在 Python 中进行网页抓取时循环网页(如谷歌搜索)
- spring-boot - Spring Boot 2.2 出现错误“带有 ID 'junit-vintage' 的 TestEngine 无法发现测试”
- python - 使用 Python 删除 csv 中的行
- php - 通过 AJAX 访问 php 文件返回 403 Forbidden
- ios - 当由“navigator.share()”(Web 共享 API)触发时,禁用 App 的 iOS 共享屏幕
- java - 线程子类中的同步方法
- ssl - 我应该如何在 BoringSSL 中使用 TLS_with_buffers_method() 与带有自签名证书的 OpenSSL 服务器通信?
- python - 在python中连接字符串和表情符号
- quarkus - Quarkus 多模块项目中的 ClassCastException