2.1 The syntax of ggplot

The reason we use the functions in ggplot2 is for consistency in the structure of it’s arguments. Here is a bare bones generic plotting function:

2.1.1 Required arguments

  • data: What data set is this plot using? This is ALWAYS the first argument.
  • aes(): This is the aestetics of the plot. What’s variable is on the x, what is on the y? Do you want to color by another variable, perhaps fill some box by the value of another variable, or group by a variable.
  • geom_THING(): Every plot has to have a geometry. What is the shape of the thing you want to plot? Do you want to plot points - use geom_points(). Want to connect those points with a line? Use geom_lines(). We will see many varieties in this lab.

2.1.2 Optional but helpful arguments

  • ggtitle: This is the overall plot title
  • xlab() and ylab() axis titles.
  • scale_xy_blah to extend limits
  • scale_fill_blah to specifying a fixed color, and change auto legend title
  • themes

For a full , and comprehensive tutorial and reference guide on how to do nearly anything in ggplot – this is by far my favorite reference http://www.cookbook-r.com/Graphs/ I reference things in there (like how to remove or change the title of a legend) constantly.