如何合并两个数据集并在 R 中绘制它们

How to merge two dataset and plot them in R

我也有 this dataset which is COVID dataset per county and state. I also have this 州人口数据集。我可能需要以某种方式将这两个数据集合并在一起并绘制病例数和人均死亡人数。如何绘制每个州的病例数和人均死亡人数?

我有以下用于合并的代码,但它一遍又一遍地重复状态并且不起作用。

{r}
#to calculate average of cases and deaths in states. 
covid %>% group_by(state) %>% summarise(ave_cases= ave(cases, na.rm = TRUE), ave_deaths= ave(deaths, na.rm = TRUE))

{r}
#to merge two data frames to have access to the population of each state.
covid<- rownames_to_column (covid, var="state")
covid_new <- covid %>% 
  left_join(US_state_pop_2020_estimate , by = c("state_territory" = "state")) %>% 
  tibble()
covid_new

也许是这样的?

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
options(scipen = 999999)

dat1 <- read.delim("~/Downloads/US_state_pop_2020_estimate.txt", sep = "\t")
dat2 <- read.csv("~/Downloads/us-counties.csv")

dat1_renamed <- rename(dat1, "state" = "state_territory")
covid_new <- left_join(dat1_renamed, dat2, by = "state")

covid_new %>%
  group_by(state) %>%
  summarise(number_of_cases = sum(cases),
            deaths_per_capita = sum(deaths / population)) %>%
  ggplot(aes(x = state, y = deaths_per_capita, fill = number_of_cases)) +
  geom_col() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
        axis.title.x = element_blank()) +
  scale_fill_viridis_c()

reprex package (v2.0.1)

于 2021-10-06 创建