Как я могу заставить facet_wrap правильно работать с этим отсортированным графом с накоплением? - PullRequest
1 голос
/ 08 октября 2019

Мне помогли отсортировать график с накоплением в следующих вопросах:

Предыдущий вопрос

Теперь я попытался добавить еще одну переменную, которую хочу использоватьfacet_wrap on.

Я пытался сгруппировать по моей новой переменной "показатели", но что-то не так, поскольку я получил эту цифру:

Wonky stacked area plot

Это новый код, единственное, что я добавил, - это новая группировка по переменной "measure" и 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)

Я ожидал график с двумя панелями, на одной из которых были нанесены данные для "Собранной площади", а на другой - данные для "Производство". Это должно выглядеть примерно так (здесь используется GGarrange, но я хочу использовать facet_wrap):

Measure as different panels

Обратите внимание, что страны разные, потому что я взялновый образец, но код тот же.

1 Ответ

1 голос
/ 08 октября 2019

Я думаю, что вы можете достичь этого, используя опцию scales ='free':

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

enter image description here

Дело в том, что вы "странная форма других, и у вас есть данные также до других plot_labels. Вы можете удалить их при необходимости, добавив функцию filter() перед ggplot.

...