将最低值分组并对该堆积的面积图进行排序 [英] Group lowest values and sort this stacked area graph
问题描述
随着时间的推移,我在一些国家/地区都有时间序列的制作.我用完整的数据制作了这个堆积面积图:
I have a time series of production in countries over time. I made this stacked area graph with the full data:
问题是它不是很可读(因为使用所有国家/地区意味着我没有传说),所以我认为我想以某种方式将生产最低的国家/地区分组,然后按产量从高到低的顺序进行排序最低.我认为基于最近几年(2017年)的值进行分组和排序将是最有意义的,因为产量通常要高得多.
The problem is that it isn't very readable (since using all countries means I can't have a legend) so I thought that I wanted to somehow group the lowest producing countries and sort the graph on the production from highest to lowest. I think that grouping and sorting based on the last years (2017) values would make the most sense, since the production is usually much higher.
这是数据的子集
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()
我不确定该怎么做,但是我从去年的排名开始.
I am not sure how to do this, but I started by making a rank based on the last year.
ordered_plot_data = plot_data %>%
filter(year == last(year)) %>%
arrange(desc(value)) %>%
mutate(rank = row_number())
所以说我想显示三个国家,其余的国家归为其他国家":
So say I wanted to have three countries shown and the rested grouped into "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年开始进行的分组,每年对它进行分组,但当然这看起来太复杂了,而且我头顶上想要一些更简单的方法来解决这个问题.
This gives me grouped data which is ordered but only for 2017. So I thought that I could use this somehow and for each year group according to the grouping I have made from 2017, but of course this just seem way too complicated and I over my head I would like some help with an easier way to solve this.
推荐答案
关键是您需要使用该顺序对因子变量进行排序.默认情况下,因子的第一级显示在顶部,因此您希望它从其他"到最高值.以下代码将为您工作!
The key thing is that you need to use that ordering to sort a factor variable. By default the first level of a factor is plotted at the top so you want it to go from "Other" through to the highest value. The following code should work for you!
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()
这篇关于将最低值分组并对该堆积的面积图进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!