Graph Basics
You have now reached the area where R really shines and definitely beats out Excel, Google Sheets, and pretty much anything else. R can make really beautiful, highly customizeable graphs.
For example, here’s a nice gallery of R plots and a package someone created that has all the Wes Anderson movies as their own color palettes (scroll down on site to see).
In R, you can basically control what every aspect of your graph looks like, from the size of the tick marks to the color of the error bars and more. If you’re someone who likes to futz with aesthetics, you might spend as long making your graphs pretty as you do on writing code that creates them.
But even nicer than beauty, R can make graphs that are easily reproducible and much quicker than it would be to create something similar in Excel.
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_histogram()
geom_boxplot()
geom_point()
geom_line()
geom_bar()
There are many other add-ons, but just those two lines of code will get you started for most types of graphs.
Here’s an example of some code that makes a very simple graph. See if you can understand what the individual lines are doing, and remember you can run each line separately or run the code chunk as a whole. One of the commands, data.frame()
, is new. Run ?data.frame
to read about it.
You should see this graph as your output. It’s really basic, but we can make it as pretty as we’d like it to be by adding more options. You’ll learn just a few in this workshop, but you can find many more online.