If you have data in two separate vectors, say X and Y, how do you make a scatterplot using base R?
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)
data(penguins)
Let’s put two columns from this dataframe into vectors called X and Y. (Don’t worry if you don’t know how to do this, just run the code).
X <- penguins$body_mass_g
Y <-penguins$bill_length_mm
We make scatterplots in R with the plot() function.
plot(Y ~ X)
We can label the x and y axis with xlab = … and ylab = …
plot(Y ~ X,
xlab = "Body mass (g)",
ylab = "Bill length (mm)")
For more information on this topic, see: https://www.statmethods.net/graphs/scatterplot.html