If we have data in separate vectors, then we can plot them into a scatterplot and then add a line of best fit to them using the lm() function.
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)
#install.packages("palmerpenguins")
library(palmerpenguins)
## Warning: package 'palmerpenguins' was built under R version 4.1.2
data(penguins)
#To set things up, we should first subset two columns from the data so we can put them into vectors.
X <- penguins$bill_length_mm
Y <- penguins$bill_depth_mm
#Bill Length
billlength_vector<- c(X)
is(billlength_vector)
## [1] "numeric" "vector"
#Bill depth
billdepth_vector<- c(Y)
is(billdepth_vector)
## [1] "numeric" "vector"
#Plot data
plot(billlength_vector ~ billdepth_vector,
xlab = "Bill Length (mm)",
ylab = "Bill Depth (mm)")
#Add line of best fit
lm(billlength_vector ~ billdepth_vector, data = penguins)
##
## Call:
## lm(formula = billlength_vector ~ billdepth_vector, data = penguins)
##
## Coefficients:
## (Intercept) billdepth_vector
## 55.0674 -0.6498
plotlobf<- plot(billlength_vector ~ billdepth_vector,
xlab = "Bill Length (mm)",
ylab = "Bill Depth (mm)"
)
abline(a=55.0674, b=-0.6498)
#First want to call up ggpubr and ggscatterplot
library(ggpubr)
## Loading required package: ggplot2
library(ggplot2)
#Use ggscatter to plot
ggscatter(data = penguins,
y = "bill_length_mm",
x = "bill_depth_mm",
xlab = "bill length (mm)",
ylab= "bill depth (mm)",
)
## Warning: Removed 2 rows containing missing values (geom_point).
#We can then add in the line of best fit as well as display the correlational coefficient
ggscatter(data = penguins,
y = "bill_length_mm",
x = "bill_depth_mm",
xlab = "bill length (mm)",
ylab= "bill depth (mm)",
add = "reg.line",
corr.coef = TRUE
)
## `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).
For more information on this topic, see https://rpubs.com/lowbrowR/palmerpenguinsplot
palmerpenguins data() plot() xlab, ylab lm() abline() ggpubr and ggplot2 ggscatter() add = reg.line