Let's see how ggplot works with the mtcars dataset. You start by plotting a scatterplot of the mpg variable and drat variable.
Basic scatter plot
library(ggplot2)
ggplot(mtcars, aes(x = drat, y = mpg)) +
geom_point()
Code Explanation
- You first pass the dataset mtcars to ggplot.
- Inside the aes() argument, you add the x-axis and y-axis.
- The + sign means you want R to keep reading the code. It makes the code more readable by breaking it.
- Use geom_point() for the geometric object.