Chapter 4 Examples

Using ggplot2 is very different from most other plotting packages, and can be a lot to take in at first. I think the best way to wrap your head around it is to see it applied in a variety of ways, and of course try to do it yourself. Let’s work through a few more common types of plots.

4.3 Box plot

Here’s a simple box plot, which relies on ggplot2 to compute some summary statistics ‘under the hood’. These are described in some detail in the geom_boxplot() documentation.

If you do not want to rely upon ggplot to compute summary statistics, you can build them yourself prior to plotting. The key to that is using the stat='indentity argument in geom_boxplot(). Here’s an example of how one might to do that.

Note how I was able to pipe directly from the summary statistics into the ggplot without writing the data to an intermediate variable. That’s not necessary, but can be useful shorthand.

4.4 Scatter plot

For the next few plots we’ll be switching over to plotting data from ocean gliders. This is a simple temperature-salinity diagram, which is essentially a scatter plot for visualizing water mass physics. In this case I’ve decided to color the points by the density of the water.

This is similar to what we’ve seen before. I have used the scale_color_viridis_c() to switch from the default ggplot colormap to a nice, perceptually uniform viridis colormap. I also am making the colorbar taller with the argument guide = guide_colourbar(barheight = 15).

4.5 Profiles

4.5.1 Temperature profiles

Here I’m going to plot vertical glider profiles. The first new things here is the scale_y_reverse() function, which simply reverses the y axis direction. The other, incredibly powerful feature I’m showing here is the facet_wrap() function. This effectively creates separate subplots for each category of the facetting variable (in this case profile_id).

4.6 Map

Mapping requires using several datasets and plotting geometries. It can be complicated, but it’s all still the same ggplot2 grammar. Here I’m using geom_polygon() to plot the coastline, geom_contour() to add a depth contour, geom_path() to draw the glider track and geom_point() to add the whale detections. The order here matters. Things added later will be plotted on top of things added earlier. The other important new concept here is the coord_quickmap(). The coord_*() functions allow you to access and alter the underlying coordinate system that ggplot is using to render your geometry. Using coord_quickmap() is a quick approximation of a spherical map projection. I’d recommend switching over to coord_map() in situations where you want a more accurate projection, and are prepared to wait for it.