I got some help to sort my stacked area graph in this questions:
Now I tried to add another variable, which I want to use facet_wrap on.
I have tried to group by my new variable "measures", but something is wrong since I get this figure:
This is the new code, the only thing I have added is the new grouping on the "measures" variable and the 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))
Code
n_countries = 5
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)
I expected a graph with two panels, one where the data for "Area harvested" was plotted and one where the data for "Production" was plotted. It should look something like this (here used GGarrange here but I want to use facet_wrap):
Note that the countries are different because I took a new sample, but the code is the same.