繁体   English   中英

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

[英]Sort line plot multiples with facet_wrap in ggplot2

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

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) 

导致: 在此处输入图片说明

理想的订单是从厄瓜多尔到阿联酋

还不清楚您所说的“最高”是什么意思,因此我认为您的意思是“在所有时间点上最高的平均值”。

我将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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM