使用ggplot2中的facet_wrap对线图进行排序

时间:2018-01-07 15:10:27

标签: r ggplot2 facet

我正在尝试将我的刻面线图从最高到最低重新排序。我知道如何使用单个条形图执行此操作,但无法弄清楚如何使用多个线条图进行此操作。这是我的示例数据:

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

我无序的ggplot2代码:

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) 

导致: enter image description here

理想的订单是从厄瓜多尔到U.A.E

1 个答案:

答案 0 :(得分:1)

你所说的“最高”并不是很清楚,所以我认为你的意思是“所有时间点的最高平均值”。

我将example_df $ Country转换为一个因子,并按平均值对其进行排序。

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)