【发布时间】:2020-12-28 16:41:22
【问题描述】:
我正在查看 R Tidy Tuesday 数据集(欧洲能源)。我已经将 Imports 和 Exports 作为比例进行了争论,并希望通过 Imports 值的上升来安排 ggplot。只是想让它看起来整洁,但似乎无法控制顺序,以查看具有下一个最大进口价值的每个后续国家。
我在代码中留下了几次尝试,但被注释掉了。提前谢谢。
library(tidyverse)
country_totals <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-08-04/country_totals.csv')
country_totals %>%
filter(!is.na(country_name)) %>%
filter(type %in% c("Imports","Exports")) %>%
group_by(country_name) %>%
mutate(country_type_ttl = sum(`2018`)) %>%
mutate(country_type_pct = `2018`/country_type_ttl) %>%
ungroup() %>%
mutate(type_hold = type) %>%
pivot_wider(names_from = type_hold, values_from = `2018`) %>%
# ggplot(aes(country_name, country_type_pct, fill = type)) +
# ggplot(aes(reorder(country_name, Imports), country_type_pct, fill = type)) +
ggplot(aes(fct_reorder(country_name, Imports), country_type_pct, fill = type)) +
geom_bar(stat = "identity") +
coord_flip()
【问题讨论】:
标签: r ggplot2 dplyr tidyverse geom-bar