Get startedGet started for free

Line plots

1. Line plots

Line plots are another very common plot type.

2. Common plot types

We'll take a look at two examples of lines plots in situations in which they are very well-suited - time series.

3. Beaver

In this first example our data set contains temperature measurements of a beaver in 10 minute intervals over 16-point-5 hours. The time variable is in a proper time format, which can be handled very well with ggplot2.

4. Beaver

Our basic line plot follows the syntax we've see so far. This is the simplest case scenario. Let's look at adding additional variables. In this case the transponder in our beaver also measured if the beaver was active or not.

5. Beaver

We can directly color each segment of our line according to another variable which is set as a color aesthetic.

6. The fish catch dataset

In the fish data set we have the global catch of 7 varieties of salmon over a 60 year period.

7. Linetype aesthetic

When we have multiple lines, we have to consider which aesthetic is more appropriate in allowing us to distinguish individual trends. Using linetype makes it difficult to distinguish individual species.

8. Size aesthetic

Using size is even worse! Don't forget to use your common sense here.

9. Color aesthetic

Using color allows for easily distinguishable groups. There are a couple other ways of showing lines.

10. Aesthetics for categorical variables

The most salient choice is color, when available, since it allows the easiest way of distinguishing between each series.

11. Fill aesthetic with geom_area()

For example, we could have used an area fill, with geom_area, which defaults to position "stack", so instead of overlapping time series, they are added together at each point. This kind of works in this situation, it really depends on how many time series you have and if that is what you want to communicate.

12. Using position = "fill"

If we use position"fill" we'll get a proportion the total capture for each fish at each time-point. Note that we'd need to change the y axis label since these are no longer counts! This kind of plot is pretty popular in looking at proportional trends over time. A difficulty with this kind of plot is that only the bottom and top groups are actually drawn on a common scale, all the other ones are irregular shapes so they can be a bit difficult to decipher.

13. geom_ribbon()

The final type of plot we'll look at is when we would want to have overlapping areas plots. In this case we'd have to use geom ribbon and force the y-min to be 0. We need to set the alpha level so that we can see the overlap. There is still a challenge in deciphering all the time series, in particularly at the bottom, where there are many overlapping series.

14. Let's practice!

We'll explore line plots in the following exercises, which will bring our discussion of the first three essential layers of ggplot2 to a close. By now you have a good core knowledge to build a wide variety of plots. I'll see you in the next chapter where we'll talk about making clean explanatory plots by using the themes layer!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.