QUESTION: “How do I add a line of best fit to a scatterplot in ggpubr?”
If you have a 2D scatterplot, how do you add a line of best fit to the graph using ggpubr?
We’ll use the “palmerpenguins” packages (https://allisonhorst.github.io/palmerpenguins/) to address this question. You’ll need to install the package with install.packages(“palmerpenguins”) if you have not done so before, call library(“palmerpenguins”), and load the data with data(penguins)
Only do this once, then comment out of the script.
#install.packages("palmerpenguins")
#install.packages("ggpubr")
library(ggpubr)
## Loading required package: ggplot2
library(palmerpenguins)
## Warning: package 'palmerpenguins' was built under R version 4.1.2
data(penguins)
ggpubr does NOT use formula notation like base R function. You have to explicitly define a y and an x variable. Additionally, the variables MUST be in quotes.
Note: Ignore any errors; these are due to NAs in the data.
Making a scatterplot in ggpubr.
We make scatterplots in ggpubr using the ggscatter () function.
Ignore any errors.
#X and Y are two columns from this dataframe; they must be in quotations marks
ggscatter(y = "bill_length_mm",
x = "bill_depth_mm",
data = penguins)
## Warning: Removed 2 rows containing missing values (geom_point).
The line has the general form
y = m*x + b
Which stats folks write as
y = B0 + B1*x
The distance from the line to each data point is called the residual.
Ignore any errors.
ggscatter(y = "bill_length_mm",
x = "bill_depth_mm",
add = "reg.line", # line of best fit
data = penguins)
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 2 rows containing non-finite values (stat_smooth).
## Warning: Removed 2 rows containing missing values (geom_point).
Additional Reading
For more information on this topic, see: http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/78-perfect-scatter-plots-with-correlation-and-marginal-histograms/
Keywords: 1. scatterplot 2. scatter plot 3. ggpubr 4. ggscatter() 5.Line of best fit 6. add = “reg.line” 8. data visualization 9. palmerpenguins