Install the necessary packages.
# install.packages("palmerpenguins")
# data(penguins)
library(ggpubr)
## Loading required package: ggplot2
## Loading required package: magrittr
Use the “palmerpenguins” data to create a dataframe including three variables. (I needed to create the vectors manually due to issues loading this package).
bill_length_mm <- c(39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, 42.0)
bill_depth_mm <- c(18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, 20.2)
body_mass_g <- c(3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, 4250)
df <- data.frame(bill_length_mm, bill_depth_mm, body_mass_g)
Use the argument “size =” to vary the point sizes of the scatter plot based on another variable. The following plots bill length against bill depth, and sizes the points based on their body mass. This allows for a third dimension of comparison.
ggscatter(x = "bill_length_mm",
y = "bill_depth_mm",
data = df,
size = "body_mass_g",
xlab = "bill length (mm)",
ylab = "bill depth (mm)",
title = "bill length vs bill depth, sized by body mass")
## Warning: Removed 1 rows containing missing values (geom_point).
Key words:
- scatter plot
- multivariate comparison
- ggpubr
- dataframe