The purpose of this exercise is to tour the table and graphics capabilities of R, and to explore methods for displaying patterns in data. If you need help with some of the commands, check the “Graphs & Tables” tab at the R tips page.

You can draw plots using commands in base R or use ggplot() from the tidyverse. Each option has passionate adherents and it is pointless to argue. Try both and see which you find most appealing and easiest to use.


Data set 1: Mammal body mass

These data were published as a data paper in Ecology and deposited in the Ecological Archives (F. A. Smith, S. K. Lyons, S. K. M. Ernest, K. E. Jones, D. M. Kaufman, T. Dayan, P. A. Marquet, J. H. Brown, and J. P. Haskell. 2003. Body mass of late Quaternary mammals. Ecology 84: 3403.) See the metadata for a description.

Most of the variables are categorical, with multiple named categories. continent includes mammals on islands (“Insular” category) whereas “Oceanic” refers to marine mammals. Body mass (in grams) is the sole numeric variable. The status variable indicates whether species is currently present in the wild (extant), extinct as of late Pleistocene (extinct), extinct within the last 300 years (historical), or an introduced species (introduction).


Read and examine the data

The original data were saved in mammals.csv file on our server here. Download the file to your computer and open in a spreadsheet program (e.g., Excel, Calc) to have a look at it.

Start R and read the contents of the file to a data frame.

Use the head() function to view the first few lines of the data frame on the screen. You’ll see that every row represents the data for a different mammal species.


Frequency tables

  1. Which continent has the greatest number of mammal species? Which has the least? Make a table of the frequency of cases on each continent (remember that the category “NA” in continent stands for North America, not missing data).

  2. You’ll notice that one of the continents is missing - North America. Yet if you open the spreadsheet you’ll see that it is in the data set. Can you figure out why it is missing? Answer is at the bottom of this page*, but see if you can figure it out yourself first. (Don’t waste too much time on it.)

  3. You’ll also notice in the frequency table for the variable continent that there’s a typo in the data. One case is shown as having the continent “Af” rather than “AF for Africa”. Fix this using the command line in R and recalculate the frequency table.

  4. How many extinct mammals are recorded in the data file? Use a frequency table to find out.

  5. Create a two-way frequency table (contingency table) showing the status of mammal species on each continent.

  6. It is easier to appreciate the relative number of extinctions on the continents if you also include the marginal sums in the table (to give the total number of species). Judging by eye, which continent seems to have the greatest number of extinctions relative to the number of extant species?


Graphing frequency distributions

  1. Plot the number of mammal species on each continent using a simple bar graph. Include a label for the y axis.

  2. The plot categories are listed in alphabetical order by default, which is arbitrary and makes the visual display less efficient than other possibilities. Redo the bar graph with the continents appearing in order of decreasing numbers of species.

  3. Generate a histogram of the body masses of mammal species. How informative is that?!

  4. Create a new variable in the mammal data frame: the log (base 10) of body mass. (See “Transform” on the R tips “Data” page if you need help with this.)

  5. Generate a histogram of log body mass. Is this more informative? Morphological data commonly require a log-transformation to analyze.

  6. Redo the previous histogram but use a bin width of 2 units. How much detail is lost? Redo the histogram but try a bin width of of 1; then try 0.5; and then 0.1. Which bin width is superior?

  7. Redo the histogram, but display probability density instead of frequency.

  8. How does the frequency distribution of log body mass depart from a normal distribution? Answer by visual examination of the histogram you just created. Now answer by examining a normal quantile plot instead. Which display is more informative? Do the data conform to a normal distribution?

  9. Optional: redraw the histogram of log body mass and superimpose a normal density curve to help visualize deviations from normality. In what ways do the data depart from normality?


Comparing frequency distributions

  1. Use a box plot to compare the distribution of body sizes (log scale most revealing) of mammals having different extinction status. Are extinct mammals similar to, larger than, or smaller than, extant mammals?

  2. Examine the previous box plot. How do the shapes of the body size distributions compare between extinct and extant mammals?

  3. Redo the previous box plot but make box width proportional to the square root of sample size. Add a title to the plot.

  4. Optional: Draw a violin plot to compare the frequency distribution of log body sizes of mammals having different extinction status. Which do you find is more revealing about the shapes of the body size distributions: box plot or violin plot?

  5. Use multiple histograms to compare the frequency distribution of log body sizes of mammals having different extinction status. Stack the panels one above the other. In this plot, how easy is it to visualize differences among treatments in the distributions compared to your previous plots?

  6. Make a table of the median log body mass of each extinction-status group of mammals. Are the values consistent with the plotted distributions?


Data set 2: Fly sex and longevity

The data are from L. Partridge and M. Farquhar (1981), Sexual activity and the lifespan of male fruitflies, Nature 294: 580-581. The experiment placed male fruit flies with varying numbers of previously-mated or virgin females to investigate how mating activity affects male lifespan. The data are in the file fruitflies.csv file on our server here.


Download and inspect

Download the file to your computer and open in a spreadsheet program to have a look at it. View the first few lines of the data frame on the screen, and familiarize yourself with the variable names.

Our goal here is to find a plot type that clearly and efficiently visualizes the patterns in the data, especially the differences among groups.


Analyze

  1. Read the data file into a new data frame.

  2. Use a strip chart to examine the distribution of longevities in the treatment groups. Try the jitter method to reduce overlap between points. If needed, adjust the size or rotation of the treatment labels so that they all fit on the graph. What pattern of differences between treatments in longevity is revealed? Males from which treatments have the highest longevity? Which have the lowest longevity?

  3. Compare the strip chart to a box plot of the same data. Is the pattern in the data as clear in both types of plot?

  4. The variable thorax stands for thorax length, which was used as a measure of body size. The measurement was included in case body size also affected longevity. Using ggplot(), produce a scatter plot of thorax length and longevity. Make longevity the response variable (i.e., plot it on the vertical axis). Is there a relationship?

  5. Redraw the scatter plot using ggplot() but this time use different symbols and/or colors for the different treatment groups. Add a legend to identify the symbols. After controlling for differences among males in size, males from which treatments have the highest longevity on average? Which have the lowest longevity?

  6. Redraw, adding regression lines to your figure, separately for each group.

  7. You can see how it can be fiendishly difficult to build a clean visual showing the pattern in the data when there are multiple groups. Redraw the figure again using just one color and symbol, but this time use facet_wrap() to plot the data in multiple panels, one per treatment group. Compare your results with those from (6). Which method shows the differences between groups most clearly?


* In this mammal data set, the symbol “NA” is used to indicate North America in the continent column. But in R, NA means missing data. To read properly, you will need to add an argument to your read.csv() command that changes the missing data code: na.strings=““ (or na=”“ if you are using read_csv() from the readr package).

 

© 2009-2026 Dolph Schluter