在此问题中,我得到了一些帮助来对堆积面积图进行排序:
现在我尝试添加另一个变量,我想在其上使用facet_wrap。
我已经尝试将新的变量“ measures”分组,但是自从得到这个数字以来,出现了一些问题:
这是新代码,我唯一添加的是关于“ measures”变量和facet_wrap的新分组。
dput
structure(list(country = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
10L), .Label = c("DR Congo", "Dominica", "Dominican Republic",
"Ecuador", "Egypt", "El Salvador", "Eswatini", "Fiji", "France",
"French Guiana"), class = "factor"), year = c(1961, 1963, 1965,
1967, 1969, 1962, 1964, 1966, 1968, 1970, 1962, 1964, 1966, 1968,
1970, 1962, 1964, 1966, 1968, 1970, 1962, 1964, 1966, 1968, 1970,
1962, 1964, 1966, 1968, 1970, 1961, 1963, 1965, 1967, 1969, 1961,
1963, 1965, 1967, 1969, 1961, 1963, 1965, 1967, 1969, 1961, 1963,
1965, 1967, 1969, 1962, 1964, 1966, 1968, 1970, 1962, 1964, 1966,
1968, 1970, 1962, 1964, 1966, 1968, 1970, 1962, 1964, 1966, 1968,
1970, 1962, 1964, 1966, 1968, 1970, 1962, 1964, 1966, 1968, 1970,
1961, 1963, 1965, 1967, 1969, 1961, 1963, 1965, 1967, 1969, 1962,
1964, 1966, 1968, 1970, 1962, 1964, 1966, 1968, 1970), measures = c("Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Area harvested", "Area harvested", "Area harvested",
"Area harvested", "Production", "Production", "Production", "Production",
"Production", "Production", "Production", "Production", "Production",
"Production", "Production", "Production", "Production", "Production",
"Production", "Production", "Production", "Production", "Production",
"Production", "Production", "Production", "Production", "Production",
"Production", "Production", "Production", "Production", "Production",
"Production", "Production", "Production", "Production", "Production",
"Production", "Production", "Production", "Production", "Production",
"Production", "Production", "Production", "Production", "Production",
"Production", "Production", "Production", "Production", "Production",
"Production"), value = c(540, 515, 515, 600, 630, 0.11, 0.115,
0.12, 0.125, 0.13, 32, 30, 25, 27.676, 26, 212.319, 299.612,
267.34, 255.28, 291.67, 769.464, 697.858, 662, 655, 633.481,
198.7, 165.754, 207.619, 199.745, 205.94, 74.964, 71.6, 72, 93,
98.884, 0.1, 0.113, 0.18, 0.809, 0.8, 980.6, 955.1, 871.3, 1015.7,
1185.2, 0.04, 0.01, 0.012, 0.015, 0.015, 0.36, 0.35, 0.39, 0.425,
0.4281, 0.00013, 0.00014, 0.00015, 0.00016, 0.00016, 0.048464,
0.043198, 0.0425, 0.04, 0.045, 0.138365, 0.12878, 0.174905, 0.173892,
0.256067, 2.00369, 1.934212, 2.376, 2.3, 2.397, 0.2139, 0.191611,
0.265914, 0.25755, 0.363078, 0.029991, 0.031751, 0.031751, 0.062595,
0.058107, 0.00022, 0.00025, 4e-04, 0.001996, 0.002, 1.86656,
2.1088, 4.3402, 5.38958, 7.491, 0.000405, 9.5e-05, 0.000111,
8.5e-05, 0.000225)), class = "data.frame", row.names = c(NA,
-100L))
代码
plot_order = plot_data %>%
mutate(country = as.character(country)) %>%
filter(year == last(year)) %>%
group_by(measures) %>%
arrange(desc(value)) %>%
mutate(rank = row_number())
final_plot <- plot_data %>%
mutate(country = as.character(country)) %>%
group_by(measures) %>%
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, measures) %>%
summarise(value = sum(value)) %>%
group_by(year)
final_plot %>%
ggplot(aes(x=year, y=value, fill=plot_label)) +
geom_area() +
facet_wrap(~measures)
我期望有两个面板的图形,一个在其中绘制“收获面积”数据,另一个在绘制“生产”数据。它应该看起来像这样:
请注意,我在这里使用了GGarrange,但我宁愿使用facet_wrap,因为我需要做很多这些事情。