#Load needed packages including interactive featureslibrary(ggplot2)library(dslabs)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
# Load datasetdata(murders)# Create ggplotp <-ggplot(murders, aes(x = population, y = total, color = region, size = population)) +geom_point(alpha =0.7) +labs(title ="Murders vs Population in US States",subtitle ="Comparing total murders and population across regions",x ="Population",y ="Total Murders",color ="Region",size ="Population",caption ="Source: DS Labs" ) +scale_color_brewer(palette ="Set1") +theme_minimal()# To make it interactiveggplotly(p)
This graph uses the murders dataset from the DS Labs package. I created a scatterplot with population on the x-axis and total murders on the y-axis. I used region as a third variable with color and population as size. I changed the theme and colors and made the graph interactive using plotly. The main insight is that states with larger populations tend to have more murders. This graph is different because it includes multiple variables and interactivity instead of a simple plot.