将最低值分组并对这个堆积面积图进行排序

Group lowest values and sort this stacked area graph

随着时间的推移,我有一个国家生产的时间序列。我用完整数据制作了这个堆积面积图:

问题是它不是很可读(因为使用所有国家意味着我不能有图例)所以我想我想以某种方式对生产最低的国家进行分组并对图表的生产进行排序从最高到最低。我认为根据过去几年(2017 年)的值进行分组和排序最有意义,因为产量通常要高得多。

这是数据的一个子集

structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L), .Label = c("Democratic People's Republic of Korea", 
"Democratic Republic of the Congo", "Dominica", "Dominican Republic", 
"Ecuador", "Egypt", "El Salvador", "Eswatini", "Fiji", "France", 
"French Guiana"), class = "factor"), year = c(1961, 1962, 1963, 
1964, 1965, 1966, 1967, 1968, 1969, 1970, 1961, 1962, 1963, 1964, 
1965, 1966, 1967, 1968, 1969, 1970, 1961, 1962, 1963, 1964, 1965, 
1966, 1967, 1968, 1969, 1970, 1961, 1962, 1963, 1964, 1965, 1966, 
1967, 1968, 1969, 1970, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 
1968, 1969, 1970, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 
1969, 1970, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 
1970, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 
1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1961, 
1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1961, 1962, 
1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970), value = c(1.245, 
1.305, 1.43, 1.505, 1.315, 1.465, 1.365, 1.32, 1.62, 1.61, 0.37, 
0.36, 0.35, 0.35, 0.35, 0.39, 0.41, 0.425, 0.43, 0.4281, 0.00013, 
0.00013, 0.00014, 0.00014, 0.00015, 0.00015, 0.00016, 0.00016, 
0.00016, 0.00016, 0.050233, 0.048464, 0.045583, 0.043198, 0.0375, 
0.0425, 0.038548, 0.04, 0.043, 0.045, 0.153047, 0.138365, 0.191953, 
0.12878, 0.191363, 0.174905, 0.227769, 0.173892, 0.211189, 0.256067, 
1.61713, 2.00369, 1.867, 1.934212, 2.141, 2.376, 2.167, 2.3, 
2.368, 2.397, 0.1763, 0.2139, 0.207077, 0.191611, 0.203006, 0.265914, 
0.20884, 0.25755, 0.278967, 0.363078, 0.029991, 0.03486, 0.031751, 
0.030481, 0.031751, 0.035017, 0.062595, 0.051709, 0.058107, 0.062595, 
0.00022, 0.00022, 0.00025, 4e-04, 4e-04, 4e-04, 0.001996, 0.00375, 
0.002, 0.000711, 2.48, 1.86656, 3.87707, 2.1088, 3.4678, 4.3402, 
4.15219, 5.38958, 5.73, 7.491, 2e-04, 0.000405, 7e-05, 9.5e-05, 
9.5e-05, 0.000111, 0.00011, 8.5e-05, 1e-04, 0.000225)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -110L))

这是我的代码

library(ggplot2)
library(tidyverse)

plot_data %>%
  ggplot(aes(x=year, y=value, fill=country)) + 
  geom_area()

我不知道该怎么做,但我首先根据去年做了一个排名。

ordered_plot_data = plot_data %>% 
  filter(year == last(year)) %>% 
  arrange(desc(value)) %>% 
  mutate(rank = row_number())

假设我想显示三个国家,其余的分组为 "others":

n_countries = 3

first_part = ordered_plot_data %>% 
  top_n(n_countries, value)

last_part = ordered_plot_data %>%
  top_n(-(length(unique(ordered_plot_data$country))-n_countries), value) %>% 
  summarise(country = "Other",
            year = first(year),
            value = sum(value),
            rank = n_countries + 1)

joined_data = rbind(first_part, last_part)

这给了我分组数据,这些数据是有序的,但仅适用于 2017 年。所以我认为我可以根据我从 2017 年开始的分组以某种方式和每年组使用它,但当然这看起来太过分了复杂,我无法理解我想要一些帮助,用更简单的方法来解决这个问题。

关键是您需要使用该顺序对因子变量进行排序。默认情况下,因子的第一水平绘制在顶部,因此您希望它从 "Other" 到最高值。以下代码应该适合您!

library(ggplot2)
library(tidyverse)

plot_order = plot_data %>% 
  mutate(country = as.character(country)) %>%
  filter(year == last(year)) %>% 
  arrange(desc(value)) %>% 
  mutate(rank = row_number())

n_countries = 3

final_plot <- plot_data %>% 
  mutate(country = as.character(country)) %>%
  mutate(plot_label = ifelse(country %in% plot_order$country[1:n_countries], country, 'Other')) %>%
  mutate(plot_label = factor(plot_label, levels = c('Other', rev(plot_order$country[1:n_countries])))) %>%
  group_by(plot_label, year) %>%
  summarise(value = sum(value)) 

final_plot %>%
  ggplot(aes(x=year, y=value, fill=plot_label)) + 
  geom_area()