R Markdown

Question: How do I make a scatterplot with different points representing different categories in ggpubr?

Data

We’ll use the “ggplot2” and “ggpubr” packages to address this question. You’ll need to install the package with install.packages(“ggplot2”) or install.packages(“ggpubr”) if you have not done so before, call the library(“ggplot2”) and library(“ggpubr”) and load the data with data(msleep)

library(ggplot2)
library(ggpubr)
data(msleep)

We’ll create new columns in the dataframe.

msleep$brainwt_log <- log(msleep$brainwt)

msleep$bodywt_log <- log(msleep$bodywt)

We can create a scatter plot that compares the log of brain weight for a species versus the log of body weight.

ggscatter(y = "brainwt_log",
          x = "bodywt_log",
          data = msleep)
## Warning: Removed 27 rows containing missing values (geom_point).

We can change the color of the points based on the type of food the species eats through the color argument. Now we have 3 different variables we can see.

ggscatter(y = "brainwt_log",
          x = "bodywt_log",
          color = "vore",    
          data = msleep)
## Warning: Removed 27 rows containing missing values (geom_point).

We can also change the size of the elements based on the total number of sleep the species gets. Now our scatterplot can show 4 different variables.

ggscatter(y = "brainwt_log",
          x = "bodywt_log",
          color = "vore", size = "sleep_total",
          data = msleep)
## Warning: Removed 27 rows containing missing values (geom_point).