divorce_margarine_chart <- divorce_margarine |>ggplot(aes(x = divorce_rate_maine, y = margarine_consumption_per_capita, color =factor(year) # use factor to make year discrete for coloring )) +geom_point(size =3, alpha =0.5) +#plot points with a little transparencygeom_smooth(method ="lm", se =FALSE, color ="red", lty =2, linewidth =0.3) +# creates the dashed red line which shows the correlationlabs(x ="Divorce Rate in Maine",y ="Margarine Consumption per Capita",color ="Year",title ="Rate of Margarine Consumption and Divorce Rate" ) +#labels both axis', legend, and title scale_color_brewer(palette ="Paired") +#Couldn't use set1 because it only supports 9 colors so i had to use a palette with 10 or more and figured out paired supports up to 12theme_minimal(base_size =14, base_family ="serif")
##Displaying the chart
divorce_margarine_chart
`geom_smooth()` using formula = 'y ~ x'
##Essay portion I used the divorce_margarine dataset from the dslabs package, which contains data on divorce rates in Maine and margarine consumption per capita over several years. In the scatterplot, the x-axis represents the divorce rate, the y-axis represents margarine consumption, and points are colored by year to show the unexpected and surprisingly really strong relationship. A dashed red trend line shows the linear correlation between the two variables. The discrete color palette ensures that each year, including 2009, is clearly visible in the legend. At first, I tried to use set1 for the color palette and it didn’t work since it only was able to represent 9 colors and 2009 wasn’t represented on the graph, the paired palette was able to support up to 12 so I chose to use it. This visualization allows us to observe trends over time, showing how changes in margarine consumption correspond to changes in divorce rates across different years. This dataset was kind of funny to me at first and that’s why I picked it, because I wondered how consumption of margarine and divorce rates were related. I ended up just searching it up online to realize it was just a funny example since the p-value is so low which shows a strong relationship between margarine consumption and divorces. This dataset can show us that correlation doesn’t always necessarily mean causation.