【问题标题】:Group lowest values and sort this stacked area graph对最小值进行分组并对这个堆积面积图进行排序
【发布时间】:2019-10-03 14:31:39
【问题描述】:

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

问题在于它的可读性不高(因为使用所有国家意味着我不能有图例)所以我想我想以某种方式对产量最低的国家进行分组,并将产量图表从最高到排序最低。我认为根据过去几年(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())

假设我想显示三个国家/地区并将其余国家归为“其他”:

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 年开始制作的分组以某种方式和每年组使用它,但当然这看起来太复杂了,而且我想通过一种更简单的方法来解决这个问题。

【问题讨论】:

    标签: r stacked-area-chart


    【解决方案1】:

    关键是您需要使用该排序来对因子变量进行排序。默认情况下,因子的第一级绘制在顶部,因此您希望它从“其他”一直到最大值。以下代码应该适合您!

    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()
    

    【讨论】:

    • 正是我想要的!谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    相关资源
    最近更新 更多