如何在 R 中为美国各州的 ProviderPerFFS 绘制 usmap(给定 2016 年条件列“年份”)

问题描述 投票:0回答:1

当我没有堆叠 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 图表。我不知道该怎么做。

r
1个回答
1
投票

通过

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)

© www.soinside.com 2019 - 2024. All rights reserved.