library(tidyverse) #Loading packages so we can make a scatterplot of the dataset we are going to use
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dslabs)
Warning: package 'dslabs' was built under R version 4.3.3
library(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
I chose the divorce_margarine data set that is included in the dslabs package. I chose this because I was interested in the divorce rates but this dataset is based in Maine.
data("divorce_margarine") # Loading the dataset so we can access and look at the contents inside
Now I am going to make a line graph of the divorce_margarine dataset so we can compare the divorce rates as the years go on.
graph <-ggplot(divorce_margarine, aes(x = year)) +geom_line(aes(y = divorce_rate_maine, color ="Divorce Rate"), size =2) +# Add the first line for the divorce rate in Maine and sets it to the y axis and sets the size of the linegeom_line(aes(y = margarine_consumption_per_capita, color ="Margarine Consumption Per Capita"), size =2) +# Add the second line for margarine consumption and sets it to the y axis and sets the size of the linescale_colour_manual("", # Manually define the colors for the lines using scale_colour_manualbreaks =c("Divorce Rate", "Margarine Consumption Per Capita"), # This helps in creating a legend with appropriate labelsvalues =c("Divorce Rate"="#07F0F0", "Margarine Consumption Per Capita"="#8D0B9E")) +labs(title ="Divorce Rate and Margarine Consumption in Maine (1960 - 2000)", # Adds a title, x, and y axis for clarity y ="Divorce Rate in Maine/ Margarine Consumption (Per 1000)",x ="Year") +theme_minimal() +# Adds a minimal theme for a clean appearancetheme(legend.position ="top") # Sets the legends position to the top
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
graph
I wanted to have an interactive plot so I added gglotly to add a little more to my line plot
interactive_plot <-ggplotly(graph) #Making the scatter plot I made above interactive and setting to a new name so we can identify them.# Display the interactive plotinteractive_plot