ggplot Syntax
The {ggplot2} Package
For graphing most people use the package {ggplot2}, which is part of what loads when you loaded the {tidyverse} library. This package is great because all graphing commands have a common syntax this follows the formula:
ggplot(data = _______, aes(x = _______, y = _______) +
geom_TYPEOFPLOT()Let’s parse that out:
ggplot()is the base command that makes graphs, it comes from the{ggplot2}packagedata= the data set we want to useaes()stands for aesthetics, it’s where you tell R what you want to be on the graphx =is where you name your x variabley =is where you name your y variablethe line ends with a
+showing you that the code continues on the next linegeom_TYPEOFPLOT()is where you specify what kind of graph you want to make, the most popular options are:geom_point()geom_line()geom_col()orgeom_bar()geom_histogram()geom_boxplot()
There are many other add-ons, but just those two lines of code will get you started for most types of graphs.