Warning: package 'dslabs' was built under R version 4.6.2
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.6.2
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
data(murders)head(murders)
state abb region population total
1 Alabama AL South 4779736 135
2 Alaska AK West 710231 19
3 Arizona AZ West 6392017 232
4 Arkansas AR South 2915918 93
5 California CA West 37253956 1257
6 Colorado CO West 5029196 65
murders <-mutate(murders, murder_rate = total / population *100000)murders <-mutate(murders, population_millions = population /1000000)
ggplot(murders, aes(x = population_millions, y = murder_rate, color = region)) +geom_point(size =3) +scale_color_brewer(palette ="Set1") +labs(title ="Population vs. Murder Rate by State",x ="Population (Millions)",y ="Murder Rate per 100,000 People",color ="Region",caption ="Source: DS Labs" ) +theme_minimal()
I used the murders dataset from dslabs to create a scatterplot comparing state populations and murder rates. I used different colors to represent each region and ggplot2 to create the graph. The graph shows how murder rates vary between states, even when their populations are similar. Unlike a basic bar graph, this visualization compares two numerical variables while showing regional differences.