2 Scatterplots: Further customization
Adding a regression line and correlation coefficient, plotting text
Postdoctoral researcher
arzbecker.1 (at) osu (dot) edu | lianarzb (at) buffalo (dot) edu
Communicative Disorders and Sciences, University at Buffalo
Adding a regression line and correlation coefficient, plotting text
# Create plot1
# Uses default "geom_smooth" settings
plot1 <- ggplot(dogs2, aes(x = weight, y = height)) +
ggtitle("plot1") + geom_point() +
scale_x_continuous(name = "Weight (lb)", limits = c(0, 180),
breaks = seq(0, 175, by = 25)) +
scale_y_continuous(name = "Height (in)", limits = c(0, 36),
breaks = seq(0, 35, by = 5)) +
geom_smooth()
print(plot1)
# Calculate the correlation coefficient
cor_coeff <- cor(dogs2$weight, dogs2$height)
# Create plot2
plot2 <- ggplot(dogs2, aes(x = weight, y = height)) +
ggtitle("plot2") + geom_point() +
scale_x_continuous(name = "Weight (lb)", limits = c(0, 180),
breaks = seq(0, 175, by = 25)) +
scale_y_continuous(name = "Height (in)", limits = c(0, 36),
breaks = seq(0, 35, by = 5)) +
geom_smooth(method = lm,
# Changes method to Linear Model from
# default Locally Estimated Scatterplot Smoothing
se = F, # Remove confidence interval
color = "red", # Color as a name or #hex code
linetype = "longdash", # Linetype as a name or numeric code
linewidth = 1.5) +
annotate("text", x = 150, y = 5, # Text placement coordinates
label = paste("r =", round(cor_coeff, 2)),
size = 4.5) # Font size
print(plot2)
# Create composite variables "friendly" and "upkeep"
dogs2$friendly <- (dogs2$affection + dogs2$kids + dogs2$dogs + dogs2$playful +
(6 - dogs2$strangers)) / 5 # Negative code strangers
dogs2$upkeep <- (dogs2$shedding + dogs2$grooming + dogs2$drooling +
dogs2$energy + dogs2$stim) / 5
# Define custom labels for the tick marks
friendly_labels <- c("2" = "Grinch", "3" = "Friendly",
"4" = "Friendlier", "5" = "Mr. Rogers")
upkeep_labels <- c("1" = "Ron\nSwanson", # "\n" creates a new line in text
"2" = "Some\nupkeep", "3" = "High\nupkeep", "4" = "Diva")
# Create plot3
plot3 <- ggplot(dogs2, aes(x = friendly, y = upkeep, color = groupF,
label = breed)) + # Specify text variable
ggtitle("plot3") + geom_point() + geom_text() + # Add breed names as text
scale_x_continuous(breaks = 2:5,
labels = friendly_labels, limits = c(2, 5)) +
scale_y_continuous(breaks = 1:4,
labels = upkeep_labels, limits = c(1, 4)) +
labs(x = "Friendliness", y = "Maintenance")
print(plot3)
# Create subset from dogs2 of only "Terrier", or "Working" groupF
dogs_subset <- subset(dogs2, groupF %in% c("Terrier", "Working"))
# Create plot4
# Same concept as plot3 but uses a subset of data and improves aesthetics
plot4 <- ggplot(dogs_subset, aes(x = friendly, y = upkeep,
color = groupF, label = breed)) +
ggtitle("plot4") + geom_point() +
geom_text_repel(size = 3.5, color = "black",
max.overlaps = Inf) + # No limit to overlapping points
scale_x_continuous(breaks = 2:5,
labels = friendly_labels, limits = c(2, 5)) +
scale_y_continuous(breaks = 1:4,
labels = upkeep_labels, limits = c(1, 4)) +
scale_color_manual(values = c("#051DD1","#E2240A")) +
labs(x = "Friendliness", y = "Maintenance",
color = "Group") # Specify legend name
print(plot4)