When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
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(tidyr)library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.5.2
As shown above I created a bar graph to visualize the findings. I also created a line in the bar graph at the fifty percent mark, to better illustrate how much support over the fifty percent mark each initiative got.
ggplot(comparison, aes(x = measure, y = support, fill = type)) +geom_col(position ="dodge") +geom_text(aes(label =percent(support, accuracy =1)),position =position_dodge(width =0.9),vjust =-0.4 ) +geom_hline(yintercept =0.5, linetype ="dashed") +scale_y_continuous(limits =c(0, 1),breaks =seq(0, 1, 0.2),labels =percent_format() ) +labs(title ="Projected vs Actual Support for Maine Ballot Measures",x ="Ballot Measure",y ="Yes Vote Share",fill ="Series" ) +theme_minimal()
As shown in the graph and codings above, I joined the maineprojections dataset with the maineresults dataset. This then allowed me to compare the projections vs the actual results of each dataset on one graph.
As shown above, I created a county variable for both the mainedemographics dataset and the maineresults dataset. I did this because I wanted to compare the results of all three ballot iniatives and demographics within each county.
In this code above I calculated the average income per each county and then caculated the support for the marijuana iniative in each county. Afterwards I combined both datasets.
ggplot(county_results,aes(x = mean_income, y = mw_support)) +geom_point(size =3) +geom_smooth(method ="lm", se =FALSE) +scale_y_continuous(labels =percent_format()) +labs(title ="Average County Income vs Marijuana Support in Maine",x ="Average Household Income",y ="Marijuana Yes Vote Share" ) +theme_minimal()
`geom_smooth()` using formula = 'y ~ x'
ggplot(county_results,aes(x = mean_income, y = mw_support)) +geom_point(size =3) +geom_text(aes(label = county), vjust =-0.6) +geom_smooth(method ="lm", se =FALSE) +scale_y_continuous(labels =percent_format()) +labs(title ="Average County Income vs Marijuana Support in Maine",x ="Average Household Income",y ="Marijuana Yes Vote Share" ) +theme_minimal()
`geom_smooth()` using formula = 'y ~ x'
In both graphs shown above I added a linear regression line to show the relationship between the two variables.
ggplot(county_results,aes(x =reorder(county, mw_support), y = mw_support)) +geom_col(fill ="steelblue") +coord_flip() +scale_y_continuous(labels =percent_format()) +labs(title ="Marijuana Support by County",x ="County",y ="Yes Vote Share" ) +theme_minimal()
ggplot(county_results,aes(x =reorder(county, mean_income), y = mean_income)) +geom_col(fill ="steelblue") +geom_text(aes(label =dollar(mean_income)), hjust =-0.1) +coord_flip() +labs(title ="Average Household Income by County",x ="County",y ="Average Household Income" ) +theme_minimal()