Chapter 6 Introduction to grammar of graphics plots

6.1 Using figures to understand and present data

Visualising data in the form of figures is the most essential element of all data analysis. Figures are used throughout the workflow of an analysis. We could break the process down into three main stages, although the boundaries between each are not hard and fast.

  1. The first figures in any analysis are usually exploratory in nature. These sort of figures will not make their way into the final write up of the analysis. However they are essential for making sense of the data. It is very difficult to understand data simply as a set of numbers. It is much easier to understand data when the numbers are turned into some sort of visual representation. Although you should keep a copy of these figures and make some notes on them they are only really for your own reference.

  2. The second class of figures are important when choosing how to apply statistics to the data. These figures are diagnostic in nature. They allow the analyst to evaluate whether the assumptions of the model (or test) are met. Some of these figures should be included in appendices and supplementary materials as they can be used to justify the choice of statistics. Some may be used in a publication if it is essential to show how the analytical method was chosen.

  3. The final class of figures are those that will always make it into the main body of the final report. The important elements to think about when designing and selecting figures for the results section are clarity and relevance. The figures need to clearly display relevant elements of the data that help the reader to draw conclusions. The complexity of the conclusions depend on the nature of the study. Inferential statistics are not always possible nor necessary in order to tell the story that is contained in the data. Often the simplest figures prove to be the most effective.

Libraries used

library(tidyverse)
library(aqm)
library(mgcv)
library(plotly)

In order to produce figures that can be really honed up to publication quality we will use the package ggplots2.

Grammar of Graphics plots (ggplots) were designed by Hadley Wickam, who also programmed dplyr. This is “tidy” approach to programming that is more powerful than base R. The syntax differs from base R syntax in various ways. It can take some time to get used to. However ggplots provides an extremely elegant framework for building really nice looking figures with comparatively few lines of code.

6.2 Simple bar charts

The simplest figures of all are probably barcharts.

Barcharts are used when the data consist of counts or percentage. Sometimes barcharts have been used to show means. These sort of barcharts with confidence intervals are inferential and are also known as “dynamite charts.” Although they are still used in some publications dynamite plots are best avoided. We will retun to this issue, but you may want to have a quick look at http://emdbolker.wikidot.com/blog:dynamite

Genuine barcharts are non inferential. In other words no statistical tests are associated with them directly. They are simply used to display information.

Let’s look at the simplest case possible where barcharts may be used. At the end of Spetember 2019 a group of Bournemouth University students monitored the traffic on the roads around the campus. They provided data on the counts of different types of vehicles passing each hour. Here is their data as they summarised it.

data(butraffic)
dt(butraffic)

These very simple data have no replication at all. Therefore there is no possibility of conducting any form of inferential statistical analysis. Sometimes a Chi Squared goodness of fit test can be used to test whether there is a significant difference between counts in each group, but this would clearly not be appropriate in this case as the differences are very apparent. However we can show the results to the reader more clearly through a figure than a table.

To form a standard barchart in gglots we first need to decide how the data will be mapped onto the elements that make up the plot. The term for this in ggplot speak is “aesthetics”- Personally I find the term aestehetics to represent mapping a bit misleading. I would instinctively assume that aesthetics refers to the colour scheme or other visual aspect of the final plot. In fact the aesthetics are the first thing to decide on, rather than the last.

The way to build a ggplot is by first forming an invisible object which represents the mapping of data onto the page. The way these data are presented can then be changed through adding differnt geometries. The only aesthetics (mappings) that you need to know about for basic usage are x,y, colour, fill, group and label. The x and y mappings coincide with axes, so are simple enough. Remember that a histogram maps onto the x axis. The y axis shows either frequency or density so is not mapped directly as a variable in the data.

The variable on the x axis in this case would be the Vehicle type. The count of the number of vehicles would be placed on the y axis. If we want to label the figure with the actual number counted (which is good practice for barcharts if feasible) we could add a label aesthetic as well.

g0<-ggplot(butraffic,aes(x=Vehicle,y=Count, label=Count)) 

Now let’s plot out the barplot. We do that by adding a geometry. There are two geometries that could be used heere. The simplest in this case is to use geom_col and add the label. We’ll assign the result to g1, then type the name g1 to plot it. That way if we want to continue modifying our plot we just add to g1.

g1<- g0 + geom_col() + geom_label()
# An alternative which does the same thing is geom_bar with identity stat.
# g1<- g0 + geom_bar(stat="identity") + geom_label() 
g1

Note that if geom_bar is used then you need to tell R to use the “identity” stat if a table of counts is used. The default stat in this case is count, i.e. geom_bar itself will form a count table from raw data.

This figure looks OK, but it would be much clearer to have the bars in a ranked order.

To do this in R we use the follwing code. First tell R to arrange the data frame according to Count. To place in ascending order use -Count. Then a little trick is used to relevel the Vehicle factor levels to match the arrangement.

butraffic %>% arrange(-Count) %>% 
  mutate(Vehicle = factor(Vehicle, Vehicle)) -> butraffic

We are going to have to set up the aesthetics again to use this, as the data have changed.

By default the background used for the first figure was grey. We can also change the look and feel of subsequent plots by setting the theme. A black and white theme might be better for printing.

theme_set(theme_bw())
g0<-ggplot(butraffic,aes(x=Vehicle,y=Count, label=Count)) 
g1<- g0 + geom_col() + geom_label()
g1

To show the results as percentages we can calculate them using another line of dplyr code.

butraffic %>% mutate(Percent=round(100*Count/sum(Count),1)) ->butraffic

Now we can try plotting with a coloured fill.

g0<-ggplot(butraffic,aes(x=Vehicle,y=Percent, fill=Vehicle, label=paste(Percent, "%", sep=""))) 
g2<-g0 + geom_col() + geom_label(show.legend = FALSE, fill="white")
g2

The RColorBrewer package has a wide range of alternative palettes if you don’t like the colours. We can also use labs to add titles and chenge the axis labels.

library(RColorBrewer)
g2 + labs(title="Traffic around Bournemouth University",caption="Source: Data collected by a Bournemouth University undergraduate student survey in September 2019",x="Type of vehicle",y="Percentage of total") +
scale_fill_brewer(palette="RdYlBu")

6.2.1 Exercise

  1. The following code chunk produces the number of votes cast for each party in Bournemouth west.
data(ge2017)
d<-ge2017
d %>% filter(constituency=="Bournemouth West") %>% dplyr::select(party,votes) ->bw
bw %>% mutate(Percent=round(100*votes/sum(votes),1)) ->bw
bw %>% arrange(-votes) %>% 
  mutate(party = factor(party, party)) -> bw
dt(bw)

Form bar charts using both counts and percentages. Try to work on the data yourself before looking at a possible solution.

g0<-ggplot(bw,aes(x=party,y=Percent, fill=party, label=paste(Percent, "%", sep=""))) 
g2<-g0 + geom_col() + 
  geom_label(show.legend = FALSE, fill="white") +
  scale_fill_discrete(type=c("darkblue", "darkred","darkorange","darkgreen","grey"))
g2 

Just to show how unhelpful pie charts are. There is a ggpie package that can make them.

library(ggpie)
bw$count<-bw$votes
ggpie(data=bw,group_key = "party",count_type = "count", fill_color=c("darkblue", "darkred","darkorange","darkgreen","grey")) ->gp
gp +labs(title="A horrible pie chart", caption="Don't make figures like this")

  1. The following code simulates responses to a question on the Likert scale
d<-data.frame(table(rand_likert()))
names(d)<-c("Response", "Count")

Form a barchart using these data.

Note that analysing a full questionaire would involve looking at the responses to many questions simultaneously.R has some additional graphical tools for this

6.3 Simple Line charts

Do not confuse genuine line charts with scatterplots with a fitted line. Line charts are usually non inferential figures (i.e. they do not show confidence intervals). A common use for a line chart is to show a time series.

We’ll extract a portion of data on rainfall from a larger data set. The code below produces a small data frame of annual rainfall measured at the Hurn meteorological station.

library(aqm)
data("met_office_2021")
met_office %>% 
  filter(station=="hurn" & Year < 2021 & Year > 1999) %>% 
  group_by(Year) %>% summarise(rain=sum(rain))-> rain

dt(rain)

We’ll set up the aesthetics. The x axis is the year, the rainfall goes on the y axis. We might want to use the number as a label.

g0<-ggplot(rain, aes(x=Year,y=rain,label=rain))

Now, adding geom_line produces a basic line chart.

g1 <- g0 + geom_line()  
g1

This might look better if the actual numbers are shown. This only works for short time series with few values. If there are more values the plot would look too cluttered.

g1 <- g1 + geom_label() 
g1

Now we might want to add a title and some text for the x and y axes.

g1<- g1 + labs(title = "Annual precipitation recorded at Hurn meterological station, Dorset", x="Year", y="Total annual precipitation in mm")
g1

This looks better, but its a bit difficult to see which year the number applies to. We can set the breaks on the continuos x axis with scale_x_continuous(breaks = )

g1 <-g1 + scale_x_continuous(breaks=1999:2020) 
g1

A final touch might be to rotate the text on x axis.

g1 <- g1 + theme(axis.text.x=element_text(angle=45, hjust=1))
g1

6.3.1 Exercise

These lines of code produce a data frame with the mean annual temperature.

met_office %>% filter(station=="hurn" & Year < 2021 & Year > 1999) %>% group_by(Year) %>% summarise(tmean=round(mean((tmax+tmin)/2),1))-> tmean

Form a line chart of the mean annual temperatures at Hurn.

6.4 Dynamic graphs

There are a growing number of packages for producing dynamic graphs in R for web pages. One that is very useful for time series is the dygraph package. This can produce figures with rolling averages, that average out a time series based on the last n observations. So if the rolling average is set to 12 months the chart shows the last 12 months.

library(dygraphs)
library(xts)
met_office %>% filter(station=="hurn")->hurn
xts(hurn$tmax,order.by = hurn$date) %>% dygraph() %>% dyRoller(rollPeriod = 12)