Reputation: 1599
I'm trying to reorder my faceted line plots from Highest to lowest. I know how to do this with single bar charts but can't figure out how to do this with multiple line plots in as facets. Here's my example data:
example_df <- structure(list(Country = structure(c(4L, 4L, 4L, 3L, 3L, 3L,
8L, 8L, 8L, 2L, 2L, 2L, 5L, 5L, 5L, 7L, 7L, 7L, 6L, 6L, 6L, 10L,
10L, 10L, 1L, 1L, 1L, 9L, 9L, 9L), .Label = c("Cameroon", "Colombia",
"Costa Rica", "Ecuador", "Guatemala", "Honduras", "Panama", "Philippines",
"U.A.E", "USA"), class = "factor"), Year = structure(c(12053,
12418, 12784, 12053, 12418, 12784, 12053, 12418, 12784, 12053,
12418, 12784, 12053, 12418, 12784, 12053, 12418, 12784, 12053,
12418, 12784, 12053, 12418, 12784, 12053, 12418, 12784, 12053,
12418, 12784), class = "Date"), Tonnes_x1000 = c(4664.814, 4521.458,
4764.193, 2042.57, 2016.687, 1775.519, 1829.384, 1797.343, 2024.322,
1424.819, 1471.394, 1621.746, 936.114, 1058.161, 1129.477, 385.32,
397.94, 352.48, 453.164, 571.686, 545.527, 427.543, 445.757,
449.647, 313.723, 294.886, 265.457, 3.337, 110, 110), mean = c(4079.75583333333,
4079.75583333333, 4079.75583333333, 2023.68983333333, 2023.68983333333,
2023.68983333333, 1524.88108333333, 1524.88108333333, 1524.88108333333,
1509.97675, 1509.97675, 1509.97675, 805.953, 805.953, 805.953,
512.769416666667, 512.769416666667, 512.769416666667, 452.0785,
452.0785, 452.0785, 415.8635, 415.8635, 415.8635, 216.352833333333,
216.352833333333, 216.352833333333, 40.1199166666667, 40.1199166666667,
40.1199166666667)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -30L), vars = "Country", drop = TRUE, .Names = c("Country",
"Year", "Tonnes_x1000", "mean"), indices = list(24:26, 9:11,
3:5, 0:2, 12:14, 18:20, 15:17, 6:8, 27:29, 21:23), group_sizes = c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), biggest_group_size = 3L, labels = structure(list(
Country = structure(1:10, .Label = c("Cameroon", "Colombia",
"Costa Rica", "Ecuador", "Guatemala", "Honduras", "Panama",
"Philippines", "U.A.E", "USA"), class = "factor")), class = "data.frame", row.names = c(NA,
-10L), vars = "Country", drop = TRUE, .Names = "Country"))
And my unordered ggplot2
code:
library(tidyverse)
example_df %>%
ggplot(aes(x = Year, y = Tonnes_x1000, colour = Country)) +
geom_line() +
scale_colour_tableau() +
scale_x_date(date_labels = "%y") +
theme_minimal() +
theme(legend.position = 0) +
labs(x = "",
y = "In 1000 Tonnes") +
facet_wrap( ~ Country, ncol = 5)
Desirable order is from Ecuador to U.A.E
Upvotes: 0
Views: 355
Reputation: 145
It's not super clear what you mean by "highest", so I assumed you meant "highest mean value over all the time points".
I turned example_df$Country into a factor, and sorted it by the mean value over time.
library(tidyverse)
### Summarize the data to find the mean of each country over time
mean_table <- example_df %>%
group_by(Country) %>%
summarize(mean = mean(Tonnes_x1000))
### Make "Country" a factor, and order it according to the means
example_df$Country <- factor(example_df$Country,
levels = mean_table$Country[rev(order(mean_table$mean))])
example_df %>%
ggplot(aes(x = Year, y = Tonnes_x1000, colour = Country)) +
geom_line() +
scale_x_date(date_labels = "%y") +
theme_minimal() +
theme(legend.position = 0) +
labs(x = "",
y = "In 1000 Tonnes") +
facet_wrap( ~ Country, ncol = 5)
Upvotes: 1