You can download this .qmd file from here. Just hit the Download Raw File button.
A recent Stat 272 project examined determinants of covid vaccination rates at the county level. Our data set contains 3053 rows (1 for each county in the US) and 14 columns; here is a quick description of the variables we’ll be using:
state = state the county is located incounty = name of the countyregion = region the state is located inmetro_status = Is the county considered “Metro” or “Non-metro”?rural_urban_code = from 1 (most urban) to 9 (most rural)perc_complete_vac = percent of county completely vaccinated as of 11/9/21tot_pop = total population in the countyvotes_Trump = number of votes for Trump in the county in 2020votes_Biden = number of votes for Biden in the county in 2020perc_Biden = percent of votes for Biden in the county in 2020ed_somecol_perc = percent with some education beyond high school (but not a Bachelor’s degree)ed_bachormore_perc = percent with a Bachelor’s degree or moreunemployment_rate_2020 = county unemployment rate in 2020median_HHincome_2019 = county’s median household income in 2019{r; warning = FALSE, message = FALSE} vaccine_data |> filter(state == "Minnesota" | state == "Iowa" | state == "Wisconsin" | state == "North Dakota" | state == "South Dakota") |> mutate(state = fct_reorder2(state, perc_Biden, perc_complete_vac)) |> ggplot(aes(x = perc_Biden, y = perc_complete_vac, color = state)) + geom_point() + geom_smooth(se = FALSE) + labs( x = "Percent of County Voted Biden (2020)", y = "P ercent of County Completely Vaccinated (2020)", color = "State County Is In" )
{r; warning = FALSE, message = FALSE} vaccine_data |> filter(state == "Minnesota" | state == "Iowa" | state == "Wisconsin" | state == "North Dakota" | state == "South Dakota") |> ggplot(aes(x = perc_Biden, y = perc_complete_vac)) + geom_point() + geom_smooth(se = FALSE) + facet_wrap(~state) + labs( x = "Percent of County Voted Biden (2020)", y = "Percent of County Completely Vaccinated (2020)" )
Describe which plot you prefer and why. What can you learn from your preferred plot?
I think that I prefer the second graph. I like that there is less of a cluster so I can more easily see the entire trend line for each state. In my opinion, the only thing that is nicer about the first graph is that it is easier to compare the extremes to the other states. From my preferred plot I can more easily see the relationship between the percent of the county that voted for Biden and the percent of the county that was completely vaccinated per state. It is easier to see that Iowa, Minnesota, and Wisconsin have a more positive linear relationship while South Dakota’s trend is more curved but still positive. North Dakota has more variety in its trend.
vaccine_data |>
group_by(state) |>
summarize(prop_above_median = mean(median_HHincome_2019 > 69560, na.rm = TRUE))
## # A tibble: 51 × 2
## state prop_above_median
## <chr> <dbl>
## 1 Alabama 0.0149
## 2 Alaska 0.5
## 3 Arizona 0
## 4 Arkansas 0.0133
## 5 California 0.46
## 6 Colorado 0.375
## 7 Connecticut 0.875
## 8 Delaware 0.333
## 9 District of Columbia 1
## 10 Florida 0.0896
## # ℹ 41 more rows
Fill in the blanks below to produce a segmented bar plot with regions ordered from highest proportion above the median to lowest.
Create a table of proportions by region to illustrate that your bar plot in (a) is in the correct order (you should find two regions that are really close when you just try to eyeball differences).
Explain why we can replace fct_relevel(region, FILL IN CODE) with
mutate(region_sort = fct_reorder(region, median_HHincome_2019 < 69560, .fun = mean))
but not
mutate(region_sort = fct_reorder(region, median_HHincome_2019 < 69560))
vaccine_data |>
mutate(HHincome_vs_national = ifelse(median_HHincome_2019 < 69560, FILL IN CODE)) |>
mutate(region_sort = fct_relevel(region, FILL IN CODE)) |>
ggplot(mapping = aes(x = region_sort, fill = HHincome_vs_national)) +
geom_bar(position = "fill")
vaccine_data |>
mutate(tot_pop_millions = tot_pop / 1000000) |>
ggplot(mapping = aes(x = tot_pop_millions)) +
geom_histogram(bins = 40) +
labs(x = "Total population in millions")
vaccine_data |>
mutate(tot_pop_millions = tot_pop %/% 1000000) |>
ggplot(mapping = aes(x = tot_pop_millions)) +
geom_histogram(bins = 40) +
labs(x = "Total population in millions")
Find the top 5 counties in terms of total population.
Plot a histogram of logged population and describe this distribution.
Plot the relationship between log population and percent vaccinated using separate colors for Metro and Non-metro counties (be sure there’s no 3rd color used for NAs). Reduce the size and transparency of each point to make the plot more readable. Describe what you can learn from this plot.
as.factor(), ifelse()).vaccine_data |>
ggplot(mapping = aes(x = rural_urban_code, y = perc_complete_vac)) +
geom_jitter() +
geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'
vaccine_data |>
ggplot(mapping = aes(x = as.factor(rural_urban_code),
y = perc_complete_vac)) +
geom_boxplot()
vaccine_data |>
ggplot(mapping = aes(x = as.factor(rural_urban_code),
y = perc_complete_vac)) +
geom_violin()
State your favorite plot, why you like it better than the other two, and what you can learn from your favorite plot.
I think that my favorite plot of the three is the box plot. I like it better than the violin plot because it makes it easier to compare the distribution. The lines make it very obvious which rural urban codes have the highest median of countys completely vaccinated. It also makes it more obvious which counties are outliers for each rural urban code and since there are many, this is very useful. I think the jitter plot is nice because it does show you all the counties, however, with so many data points it can be a little overwhelming. From the box plot, you can see that the counties that are the least urban also have the highest percentage of their population completely vaccinated, while the most rural had a lower percentage of their population completely vaccinated.
Conclusions that can be drawn from this plot are that Virginia, Georgia, and North Carolina are the three states that had the most variety in the percentage of people that voted for Biden based on county. Tennessee and Missouri on the other hand had almost the same percentage of people vote for Biden, regardless of which county they lived in.
vaccine_data |>
filter(!is.na(perc_Biden)) |>
mutate(big_states = fct_lump(state, n = 10)) |>
group_by(big_states) |>
summarize(IQR_Biden = IQR(perc_Biden)) |>
mutate(big_states = fct_reorder(big_states, IQR_Biden)) |>
ggplot() +
geom_point(aes(x = IQR_Biden, y = big_states))
new_vaccine_data <- vaccine_data |>
group_by(state) |>
summarise(proportion_metro = sum(metro_status == "Metro") / n(),
total_state_pop = sum(tot_pop),
n_rural_urban_codes_per_state = n_distinct(rural_urban_code),
median_unemployment_rate = median(unemployment_rate_2020)) |>
arrange(desc(total_state_pop))
new_vaccine_data
## # A tibble: 51 × 5
## state proportion_metro total_state_pop n_rural_urban_codes_per_state
## <chr> <dbl> <dbl> <int>
## 1 California 0.74 39418470 7
## 2 Texas 0.324 28995712 9
## 3 Florida 0.657 21477737 7
## 4 New York 0.613 19453561 8
## 5 Pennsylvania 0.552 12801989 8
## 6 Illinois 0.392 12671821 9
## 7 Ohio 0.432 11689100 7
## 8 North Carolina 0.46 10488084 9
## 9 Georgia 0.477 10443765 9
## 10 Michigan 0.313 9986857 9
## # ℹ 41 more rows
## # ℹ 1 more variable: median_unemployment_rate <dbl>
rural_urban_codes represented among the state’s counties (there are 9 possible)rural_urban_codes in a state, but a single linear trend should be fit to all points. What can you conclude from the plot?new_vaccine_data |>
ggplot(aes(x = proportion_metro, y = median_unemployment_rate)) +
geom_point(aes(color = n_rural_urban_codes_per_state)) +
geom_smooth(method = "lm", se = FALSE) +
labs(
x = "Proportion of Metro Counties (In a Given State)",
y = "Median Unemployment Rate (In a Given State)",
color = "Number of Different Rural Urban Codes Represented (In a Given State"
)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
From this plot I can conclude that
In this next section, we consider a few variables that could have been included in our data set, but were NOT. Thus, you won’t be able to write and test code, but you nevertheless should be able to use your knowledge of the tidyverse to answer these questions.
Here are the hypothetical variables:
# Hypothetical R chunk 1
temp <- vaccine_data |>
mutate(new_perc_vac = ifelse(perc_complete_vac > 95, NA, perc_complete_vac),
MD_group = cut_number(people_per_MD, 3)) |>
group_by(MD_group) |>
summarise(n = n(),
mean_perc_vac = mean(new_perc_vac, na.rm = TRUE),
mean_white = mean(perc_white, na.rm = TRUE))
temp created above. What would be the dimensions? What do rows and columns represent?The tibble above would have 3 rows and 4 columns. Each row would represent an MD Group, which makes up of 1/3 of the population. The columns would be MD Group, n, mean_perc_vac, and mean_white.
new_perc_vac = ifelse(perc_complete_vac > 95, NA, perc_complete_vac) with new_perc_vac = ifelse(perc_complete_vac > 95, perc_complete_vac, NA)?If we replaced new_perc_vac = ifelse(perc_complete_vac > 95, NA, perc_complete_vac) with new_perc_vac = ifelse(perc_complete_vac > 95, perc_complete_vac, NA) the rows that have a higher than a 95 in the perc_complete_vac column would remain the same while the rows that have less than or equal to 95 in the perc_complete_vac column would be changed to NA. In the previous code this was switched so the rows with higher than a 95 in the perc_complete_vac column got switched to NA and those with a value less than or equal to 95 remained the same.
mean_white = mean(perc_white, na.rm = TRUE) with mean_white = mean(perc_white)?If we replaced mean_white = mean(perc_white, na.rm = TRUE) with mean_white = mean(perc_white)then the rows with no value in the column mean_white (looks like NA) would still be included in the dataset.
group_by(MD_group)?If we removed the group_by(MD_group) then there would only be one row in the final dataset because it would find the summary statistics for all counties in the dataset and not be split up into groups.
# Hypothetical R chunk 2
ggplot(data = vaccine_data) +
geom_point(mapping = aes(x = perc_over_65, y = perc_complete_vac,
color = HR_party)) +
geom_smooth()
temp <- vaccine_data |>
group_by(HR_party) |>
summarise(var1 = n()) |>
arrange(desc(var1)) |>
slice_head(n = 3)
vaccine_data |>
ggplot(mapping = aes(x = fct_reorder(HR_party, perc_over_65, .fun = median),
y = perc_over_65)) +
geom_boxplot()
The first plot would produce an error because the mapping for the x and y values are done for the scatter plot in geom_point but there are no x and y values for the geom_smooth.
temp created above. What would be the dimensions? What do rows and columns represent?The tibble temp would have 2 columns and 3 rows. It would include the 3 HR_Party ’s with the most counties. The top one would have the most counties, the second one has the second most, and third has third most. The first column would have the HR_Party and the second would have how many counties in that given HR_Party.
fct_reorder(HR_party, perc_over_65, .fun = median) with HR_party?I believe if we replaced fct_reorder(HR_party, perc_over_65, .fun = median) with HR_party that the HR_Party on the x-axis would no longer be in order from left to right based on the median perc_over_65. Instead the box plots would likely be ordered in the original datasets order.
# Hypothetical R chunk 3
vaccine_data |>
filter(!is.na(people_per_MD)) |>
mutate(state_lump = fct_lump(state, n = 4)) |>
group_by(state_lump, rural_urban_code) |>
summarise(mean_people_per_MD = mean(people_per_MD)) |>
ggplot(mapping = aes(x = rural_urban_code, y = mean_people_per_MD,
colour = fct_reorder2(state_lump, rural_urban_code, mean_people_per_MD))) +
geom_line()
There would be 3 columns and 45 rows. Each row represents a combination of state and rural_urban_code. One column represents states, the other rural_urban_code, and the last one represents the mean people_per_md for each state and rural_urban_code combo.
The plot created above is a line plot. On the x-axis is the rural_urban_code and on the y axis is the mean_people_per_MD. There will be 5 different lines, all of them a different color. Four of the lines will represent the top 4 states and the last line will be other. The legend for the color will be ordered based on which line is the highest up on the farthest right point (aka which state has the highest mean_people_per_MD at the highest rural_urban_code (9).
filter(!is.na(people_per_MD))?If we removed filter(!is.na(people_per_MD)) from the code, then any group (state and rural_urban_code combo) that contains an NA in the people_per_MD will have a mean_people_per_MD that is also NA. This would cause breaks in the line for anywhere where the NA occurs.
fct_reorder2(state_lump, rural_urban_code, mean_people_per_MD) with state_lump?If we replaced fct_reorder2(state_lump, rural_urban_code, mean_people_per_MD) with state_lump then the legend would not be organized based on which state had the highest mean_people_per_MD at the highest rural_urban_code value but instead it would listed in the original order in the dataset. In this case, the original order would be the state with the most counties from greatest to least.