Dot Plots In R

Hi. Here is a mini-project that I have been working on. I wanted to play around with dot plots in R and this is what I came up with.


Table Of Contents

Butterfat Content Data

Dot Plots Using ggplot2

Cleavland Dot Plots Of Average Butterfat Content By Cow Breed And Age Using dplyr and ggplot2

References


Butterfat Content Data

We first load the faraway dataset library and the ggplot2 package in R.

I save the butterfat data into a variable called butterfat_data. The head() and tail() functions are used to preview the data.

The data structure can be examined using str().

The R documentation for the dataset can be found in the faraway library and in the dataset butterfat. I have attached a screenshot image below.

 


Dot Plots Using ggplot2

Creating dot plots using ggplot2 is not difficult. The tricky parts might be customizing the labels and aesthetics. Here is the code and output below.

An alternative to the above plot would be switching the Breed Type from the horizontal axis to the vertical axis. Also, the Butterfat Percentage would transition from vertical to horizontal. Here is the code and output.


Cleavland Dot Plots Of Breed Averages Using dplyr and ggplot2

A Cleavland dot plot is an alternative to the bar graph. Instead of having a bar, a single dot is used instead. This type of dot plot can be useful as it reduces visual clutter from all the bars.

I want to plot averages for each breed. Each average (for each group and for each age group) is represented by a single dot. I would have a total of ten dots (because of 5 breeds multiplied by the two age groups).

In R, I load the dplyr package for data manipulation and data wrangling.

I do not want to go into great detail about the dplyr functions here. The code below creates a new dataset variable called butterfat_avg where butterfat_data contains the Breed, Age and the average butterfat content percentage as the three columns.

It makes sense how this new data table has 10 rows/observations since there are 5 breeds and 2 age groups.


The code and output for the dot plots in R using ggplot2 can be found below.

The code and output for the sideways version of the plot above can be found below.

You can see that the average butterfat content percentage from the 2 year Holstein-Fresian cow is not much different than the average butterfat content percentage from the mature Holstein-Fresian cow.

There is rather large difference between the average butterfat content percentage of the 2 year Jersey cow and the mature Jersey cow.


References

  • R Graphics Cookbook by Winston Chang (2012)
  • https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf

Leave a Reply