1

I got some help to sort my stacked area graph in this questions:

Earlier question

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:

Wonky stacked area plot

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

Measure as different panels

Note that the countries are different because I took a new sample, but the code is the same.

Robin Lindström
  • 622
  • 4
  • 15

1 Answers1

1

I think you can achieve it using the scales ='free' option:

library(tidyverse)
final_plot %>% 
  ggplot(aes(x=year, y=value, fill=plot_label)) + 
  geom_area() +
  facet_wrap(~measures,scales = 'free' )

enter image description here

The fact is also that you've a "weird" shape of the Others, and you have data also before the other plot_labels. You can delete them if needed adding a filter() function before the ggplot.

s__
  • 9,270
  • 3
  • 27
  • 45
  • Thank you! But the problem is more about the weird shape of the other data, because the other data is not supposed to look like that. I think the problem is the way I try to do the grouping, but I cant find the error. I have added a plot using ggarrange of how it should look like. – Robin Lindström Oct 11 '19 at 08:51
  • Try to check this: `table(final_plot$plot_label,final_plot$year)`. You have a lot of missing points, and it seems to [mess up the plot](https://stackoverflow.com/questions/29807080/geom-area-not-stacking-ggplot). To me the grouping seems correct. – s__ Oct 11 '19 at 11:12