如何限制 facet_wrap() 中的因子数量以仅绘制前 N 个因子?
How to limit the number of factors in facet_wrap() to plot for only top N factors?
我正在区级处理 Covid 数据,并尝试使用 facet_wrap()
.
为 区 绘制案例 timeseries
有些州有很多地区,所有地区都装不下,所以我想 限制 facet_wrap 前N个区
我已经尝试 重新排序 facet_wrap()
fct_reorder(Districts)
但这只会重新排序并为所有地区生成地块。
有没有一种方法可以获得 fct_reorder(Districts)
中的 前 N 个级别 并仅绘制那些 前 N 个 或者如果有任何选项可以控制 facet_wrap
中的 面数 ?
df:
library(tidyverse)
library(lubridate)
file_url <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/df_districts.csv"
df_districts <- read.csv(url(file_url))
df_districts <- df_districts %>%
mutate(Date = ymd(Date))
我试过了:
df_districts %>%
filter(State == "Rajasthan",
Date != max(Date),
!is.na(Daily_confirmed)) %>%
# group_by(District, Date) %>%
# slice_max(order_by = Daily_confirmed, n = 20) %>%
mutate(District = fct_reorder(District, Daily_confirmed,
.fun = max, .desc = TRUE)) %>%
ggplot(aes(x = Date, y = Daily_confirmed)) +
geom_line(size = 1) +
facet_wrap(~District)
一种方法是根据您需要的任何优先级(例如,sum(Daily_confirmed)
)总结 table,然后按排序后的变量取前“n”。
df_districts %>%
group_by(District) %>%
summarize(daily = sum(Daily_confirmed)) %>%
slice_max(daily, n = 10)
# # A tibble: 10 x 2
# District daily
# <chr> <int>
# 1 Jaipur 99843
# 2 Jodhpur 72443
# 3 Kota 39442
# 4 Alwar 34650
# 5 Udaipur 31297
# 6 Bikaner 26144
# 7 Ajmer 25866
# 8 Bhilwara 19922
# 9 Pali 16589
# 10 Sikar 15031
使用此数据,我们可以 left_join
返回 District
的原始数据(如果需要,先删除 daily
),您的子集将只包含这些地区。
out <- df_districts %>%
group_by(District) %>%
summarize(daily = sum(Daily_confirmed)) %>%
slice_max(daily, n = 10) %>%
select(-daily) %>%
left_join(df_districts, by = "District")
out
# # A tibble: 3,660 x 11
# District Date State Confirmed Recovered Deceased Other Tested Daily_confirmed Daily_Recovered Daily_Deceased
# <chr> <date> <chr> <int> <int> <int> <int> <int> <int> <int> <int>
# 1 Jaipur 2021-04-27 Rajasthan 100651 68325 659 0 968783 3289 989 21
# 2 Jaipur 2021-04-26 Rajasthan 97362 67336 638 0 968783 2878 961 11
# 3 Jaipur 2021-04-25 Rajasthan 94484 66375 627 0 968783 3145 648 13
# 4 Jaipur 2021-04-24 Rajasthan 91339 65727 614 0 968783 3260 657 9
# 5 Jaipur 2021-04-23 Rajasthan 88079 65070 605 0 968783 3036 767 13
# 6 Jaipur 2021-04-22 Rajasthan 85043 64303 592 0 968783 2317 658 11
# 7 Jaipur 2021-04-21 Rajasthan 82726 63645 581 0 968783 3101 551 5
# 8 Jaipur 2021-04-20 Rajasthan 79625 63094 576 0 968783 1875 413 9
# 9 Jaipur 2021-04-19 Rajasthan 77750 62681 567 0 968783 2011 528 11
# 10 Jaipur 2021-04-18 Rajasthan 75739 62153 556 0 968783 1963 258 4
# # ... with 3,650 more rows
为了比较,
table(df_districts$District)
# Ajmer Alwar Banswara Baran Barmer Bharatpur Bhilwara Bikaner
# 366 366 366 362 366 366 366 366
# BSF Camp Bundi Chittorgarh Churu Dausa Dholpur Dungarpur Evacuees
# 356 335 366 366 366 366 366 366
# Ganganagar Hanumangarh Italians Jaipur Jaisalmer Jalore Jhalawar Jhunjhunu
# 342 366 366 366 366 356 366 366
# Jodhpur Karauli Kota Nagaur Other State Pali Pratapgarh Rajsamand
# 366 366 366 366 366 366 366 366
# Sawai Madhopur Sikar Sirohi Tonk Udaipur
# 366 366 355 366 366
table(out$District)
# Ajmer Alwar Bhilwara Bikaner Jaipur Jodhpur Kota Pali Sikar Udaipur
# 366 366 366 366 366 366 366 366 366 366
我正在区级处理 Covid 数据,并尝试使用 facet_wrap()
.
timeseries
有些州有很多地区,所有地区都装不下,所以我想 限制 facet_wrap 前N个区
我已经尝试 重新排序 facet_wrap()
fct_reorder(Districts)
但这只会重新排序并为所有地区生成地块。
有没有一种方法可以获得 fct_reorder(Districts)
中的 前 N 个级别 并仅绘制那些 前 N 个 或者如果有任何选项可以控制 facet_wrap
中的 面数 ?
df:
library(tidyverse)
library(lubridate)
file_url <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/df_districts.csv"
df_districts <- read.csv(url(file_url))
df_districts <- df_districts %>%
mutate(Date = ymd(Date))
我试过了:
df_districts %>%
filter(State == "Rajasthan",
Date != max(Date),
!is.na(Daily_confirmed)) %>%
# group_by(District, Date) %>%
# slice_max(order_by = Daily_confirmed, n = 20) %>%
mutate(District = fct_reorder(District, Daily_confirmed,
.fun = max, .desc = TRUE)) %>%
ggplot(aes(x = Date, y = Daily_confirmed)) +
geom_line(size = 1) +
facet_wrap(~District)
一种方法是根据您需要的任何优先级(例如,sum(Daily_confirmed)
)总结 table,然后按排序后的变量取前“n”。
df_districts %>%
group_by(District) %>%
summarize(daily = sum(Daily_confirmed)) %>%
slice_max(daily, n = 10)
# # A tibble: 10 x 2
# District daily
# <chr> <int>
# 1 Jaipur 99843
# 2 Jodhpur 72443
# 3 Kota 39442
# 4 Alwar 34650
# 5 Udaipur 31297
# 6 Bikaner 26144
# 7 Ajmer 25866
# 8 Bhilwara 19922
# 9 Pali 16589
# 10 Sikar 15031
使用此数据,我们可以 left_join
返回 District
的原始数据(如果需要,先删除 daily
),您的子集将只包含这些地区。
out <- df_districts %>%
group_by(District) %>%
summarize(daily = sum(Daily_confirmed)) %>%
slice_max(daily, n = 10) %>%
select(-daily) %>%
left_join(df_districts, by = "District")
out
# # A tibble: 3,660 x 11
# District Date State Confirmed Recovered Deceased Other Tested Daily_confirmed Daily_Recovered Daily_Deceased
# <chr> <date> <chr> <int> <int> <int> <int> <int> <int> <int> <int>
# 1 Jaipur 2021-04-27 Rajasthan 100651 68325 659 0 968783 3289 989 21
# 2 Jaipur 2021-04-26 Rajasthan 97362 67336 638 0 968783 2878 961 11
# 3 Jaipur 2021-04-25 Rajasthan 94484 66375 627 0 968783 3145 648 13
# 4 Jaipur 2021-04-24 Rajasthan 91339 65727 614 0 968783 3260 657 9
# 5 Jaipur 2021-04-23 Rajasthan 88079 65070 605 0 968783 3036 767 13
# 6 Jaipur 2021-04-22 Rajasthan 85043 64303 592 0 968783 2317 658 11
# 7 Jaipur 2021-04-21 Rajasthan 82726 63645 581 0 968783 3101 551 5
# 8 Jaipur 2021-04-20 Rajasthan 79625 63094 576 0 968783 1875 413 9
# 9 Jaipur 2021-04-19 Rajasthan 77750 62681 567 0 968783 2011 528 11
# 10 Jaipur 2021-04-18 Rajasthan 75739 62153 556 0 968783 1963 258 4
# # ... with 3,650 more rows
为了比较,
table(df_districts$District)
# Ajmer Alwar Banswara Baran Barmer Bharatpur Bhilwara Bikaner
# 366 366 366 362 366 366 366 366
# BSF Camp Bundi Chittorgarh Churu Dausa Dholpur Dungarpur Evacuees
# 356 335 366 366 366 366 366 366
# Ganganagar Hanumangarh Italians Jaipur Jaisalmer Jalore Jhalawar Jhunjhunu
# 342 366 366 366 366 356 366 366
# Jodhpur Karauli Kota Nagaur Other State Pali Pratapgarh Rajsamand
# 366 366 366 366 366 366 366 366
# Sawai Madhopur Sikar Sirohi Tonk Udaipur
# 366 366 355 366 366
table(out$District)
# Ajmer Alwar Bhilwara Bikaner Jaipur Jodhpur Kota Pali Sikar Udaipur
# 366 366 366 366 366 366 366 366 366 366