Back to Browse

ggplot2 tutorial: ggplot2 Layers

24.6K views
Nov 10, 2016
2:50

Learn more about ggplot2 layers in R: https://www.datacamp.com/courses/data-visualization-with-ggplot2-part-2 Now that we have some idea about the different grammatical elements of graphics, let's see how this works in practice. The grammar of graphic is implemented in R using the ggplot2 package, which was one of the first packages developed by the prolific statistician and R programmer Hadley Wickham. Essentially, we construct plots by layering grammatical elements on top of each other and use aesthetic mappings to define our visualisations. We are going to go through each grammatical element in depth in this and the next course. Here I'll introduce a data set which will be used throughout the videos and we'll go over some simple examples. The first layer is data. Obviously we need some data to plot. I'm going to use several different data sets in the course videos, one of which is the classic iris data set collected by Edgar Anderson in the 1930s and thereafter popularised by RA Fisher. The data set contains information on three iris species, setosa, virginica and versicolor. Four mearurements were taken from each plant - the petal length and with and the sepal length and width. You're probably familiar with petals, they're the colourful part of a flower. Sepals are the outter leaves of the flower, they are typicall green, but in this case they are colourful. There are 50 specimens of each species. The data is stored in an object called iris, there ar five variables: the species and one for each of the properties which were measured. The next layer is aesthetics, which tells us which scales we should map our data onto. This is where the second main component of the grammar of graphics comes into play. On top of layering the grammatical elements, it's here that we establish our aesthetic mappings. In this case we are going to make a scatter plot so we're going to map the Sepal.Length onto the X aesthetic and the Sepal.Width onto the Y aesthetic. The third essential layer is allows us to choose that geometry, that means how the plot will look. After we've established our three essential layers, we have enough instructions to make a basic scatter plot plot. It's pretty rough, so to get a more meaningful and cleaner visualisaiton, we'll have to use the other layers. The next layer we'll use is facets, which dictate how to split up our plot. In this case we want to make three separate plots one for each of three species under consideration. The statistics layer can be use to calculate and add many different parameters. For example, here we've chosen to add a linear model to each of the three subplots. Next comes the coordinate layer, which allows us to specify the precise dimensions of the plot. Here we've cleaned up the labelling and the scalling of both the x and x axes. And finally the theme layer controls all the non-data ink on our plot. Which allows us to get a nice looking, meaningful and publication quality plot directly in R. Let's explore these concepts further in the exercises.

Download

0 formats

No download links available.

ggplot2 tutorial: ggplot2 Layers | NatokHD