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 murders data set that is included in the dslabs package.
data("murders") # Loading the dataset so we can access and look at the contents inside
Now I am going to make a dot graph of the murders dataset so we can compare the murder rates in the different regions accros the U.S.
# Creating a scatter plot with ggplot2graph <-ggplot(murders, aes(x = population, y = total, text = state, size = total, color = region)) +geom_point(alpha =0.6) +# Makes the points semi-transparentscale_size(range =c(1, 20), name ="Total Murders") +# Adjust the size scale for visibilityscale_color_brewer(palette ="Set1", name ="Region") +# Use a color brewer palette for regionslabs(title ="Murder Rates vs. Population by State and Region", # Creates names for the title, x, and yx ="Population",y ="Total Murders") +theme_light(base_size =14) +# Use a light theme with a base font size of 14theme(legend.position ="right") # Place the legend on the right sidegraph
I wanted to have an interactive plot so we could see the states and their stats 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