Murders scatter plot
library(ggplot2)
library(dslabs)
## Warning: package 'dslabs' was built under R version 4.3.3
data("murders")
summary(murders)
## state abb region population
## Length:51 Length:51 Northeast : 9 Min. : 563626
## Class :character Class :character South :17 1st Qu.: 1696962
## Mode :character Mode :character North Central:12 Median : 4339367
## West :13 Mean : 6075769
## 3rd Qu.: 6636084
## Max. :37253956
## total
## Min. : 2.0
## 1st Qu.: 24.5
## Median : 97.0
## Mean : 184.4
## 3rd Qu.: 268.0
## Max. :1257.0
Here the visulazation using a scatterplot
ggplot(murders, aes(x = population, y = total, color = region)) +
geom_point(alpha = 0.7) + # Add points with some transparency
labs(x = "Population", y = "Total Murders", title = "Population vs. Total Murders by Region") +
scale_color_manual(values = c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728"),
name = "Region",
labels = c("Northeast", "South", "Midwest", "West")) + # Custom colors and labels
theme_minimal() + # Change to a minimal theme
theme(legend.position = "bottom") # Position legend at the bottom
#Explanation:
ggplot(murders, aes(x = population, y = total, color = region)): Sets up the basic ggplot object with the dataset and aesthetics mappings.
geom_point(alpha = 0.7): Adds points to the plot with some transparency for better visualization of overlapping points.
labs(): Sets the labels for the x-axis, y-axis, and title.
scale_color_manual(): Manually specifies the colors for different regions and sets the legend title and labels accordingly.
theme_minimal(): Applies a minimal theme to the plot for a clean look.
theme(legend.position = “bottom”): Adjusts the position of the legend to the bottom of the plot for better layout.
This graph visualizes the relationship between population size and the total number of murders, with each point representing a state colored by its region. By changing the colors and adjusting the theme, we enhance the readability and aesthetics of the graph compared to the default ggplot style.
# the graph with modifications
ggplot(murders, aes(x = population, y = total, color = region)) +
geom_point(alpha = 0.7) + # Add points with some transparency
labs(x = "Population (in millions)", y = "Total Murders", title = "Population vs. Total Murders by Region in 2010") +
scale_color_brewer(palette = "Set1", name = "Region") + # Use a different color palette
theme_bw() + # Change to a black and white theme
theme(legend.position = "bottom") + # Position legend at the bottom
guides(color = guide_legend(title = "Region")) # Add a title to the legend
Explanation of modifications:
Changed the title to “Population vs. Total Murders by Region in 2010” for clarity. Adjusted the x-axis label to “Population (in millions)” for better understanding. Utilized the scale_color_brewer() function with the “Set1” palette to change the color scheme. Changed the theme to theme_bw() for a black and white appearance. Added a title to the legend using the guides() function for better interpretation.