当我没有堆叠 2017 年数据集时,我能够使用 usmap 包绘制 ProviderPerFFS:
plot_usmap(data = df2, values = "ProviderPerFFS", color = "red") +
scale_fill_continuous(name = "ProviderPerFFS10k(2016)", type = "viridis", label = scales::comma) +
theme(legend.position = "right")
现在我有一个新列,上面写着 2017 年或 2016 年。我想分别绘制 2017 年和 2016 年的 usmap 图表。我不知道该怎么做。
通过
subset
或 dplyr::filter
或 ... 按要绘制的年份对数据进行子集化或过滤,或者如果您想在一个图中绘制两个年份,则使用分面。
使用一些随机示例数据尝试以下操作:
library(usmap)
library(ggplot2)
library(dplyr, warn = FALSE)
# example data
set.seed(42)
df <- usmapdata::fips_data(regions = "counties") %>%
select(fips)
df <- list(
`2016` = mutate(df, ProviderPerFFS = rnorm(nrow(df))),
`2017` = mutate(df, ProviderPerFFS = rnorm(nrow(df)))
) %>%
bind_rows(.id = "Year")
# Single year
plot_usmap(
data = subset(df, Year == 2016),
values = "ProviderPerFFS", color = "red"
) +
scale_fill_continuous(
name = "ProviderPerFFS10k(2016)",
type = "viridis", label = scales::comma
) +
theme(legend.position = "right")
# Facet by year
plot_usmap(data = df, values = "ProviderPerFFS", color = "red") +
scale_fill_continuous(name = "ProviderPerFFS10k(2016)", type = "viridis", label = scales::comma) +
theme(legend.position = "right") +
facet_wrap(~Year)