If you would like to return to information from the previous section, please click here.
There are a number of ways to visualise trends of coral reef monitoring data, depending on the level of information contained in the data, variables, and spatial and temporal extent.
In this module, we will be exploring a number of ways to visualise
data and provide the basic skills to make plots using
ggplot2
and related tools.
The code for the “status” example can be found here:
analysis_code/examples/visualisation/plot_percent_cover_acosa.R
Code for the “trends” can be found here:
analysis_code/examples/visualisation/plot_kenya_cover.R
From our previous example, we created some initial summaries of the data to reduce the complexity of the taxonomic groups and obtain quadrat means for each transect.
With these data will will start visualising:
# create visual
sessiles_dat_summary %>%
dplyr::filter(!Grouping %>% is.na()) %>%
ggplot(aes(Grouping, Value_mean)) +
geom_boxplot(fill = "blue",
alpha = 0.7) +
theme_bw() +
xlab("Functional Group") +
ylab("Mean Percent Cover")
Which gives us something like:
We can see from this plot is that this region “ACOSA” is
dominated by Macroalgae
, Non-living
subtrata,
with some Live coral
. Note that the y-axis is “Mean Percent
Cover” at the transect level, so we will probably need to check why
there are values higher than 100%. We will pick this up later
as part of our Exercises, but in essence this
figure has provided us with our first data quality check!
A few things to note in the code:
<NA>
taxa groups and piping the result to ggplot()
ggplot()
starts by defining an “aesthetic”
(i.e. aes()
which defines the x and
y values). In this case it is the taxonomic
Grouping
and the mean Value
.+
) a geom
, which in this case is a
boxplot. However, it could be a number of things
(e.g. geom_point
).geom
, we then set fill values, transparency, and
other aesthetics or mappings (if required). We will come back
to this in later exercises.geom
s, we then add the
theme_bw()
, which simplifies the figure to a
“black-and-white”xlab()
and
ylab()
As a way to standardising GCRMN coding projects, we try to standardise this “layering” as much as possible. It is helpful to have elements to be modified (e.g. data, axes, themes) at the beginning or end of a block of code so it is easier to find if modifications are needed.
As one of the key functions of the GCRMN is to report on the trends of coral reefs through time, we can apply the similar data aggregation and plotting for a time series from Kenya.
Note that in this example, we are filtering for
groups_of_interest
, which is “hard coral”.
# visualise
kenya_cover %>%
dplyr::filter(level1_code %in% groups_of_interest) %>%
group_by(Year,
Site) %>%
summarise(mean_cover = mean_cover %>% mean(na.rm = TRUE)) %>%
group_by(Year) %>%
summarise(sd = mean_cover %>% sd(na.rm = TRUE),
mean_cover = mean_cover %>% mean(na.rm = TRUE)) %>%
mutate(sd_upper = mean_cover + sd,
sd_lower = mean_cover - sd) %>%
ggplot(aes(Year, mean_cover)) +
geom_line(alpha = 0.7) +
geom_point(size = 2.0, alpha = 0.7) +
geom_errorbar(aes(ymax = sd_upper,
ymin = sd_lower)) +
theme_bw() +
ylab("Mean percent cover") +
ggtitle("Kenya") +
theme(plot.title = element_text(hjust = 0.5))
The result looks something like this:
In this example, we are conducting a similar aggregation by
Year
and Site
to get means of
Station
s and then taking the mean()
by
Year
. In order to add the error bars, we use
mutate()
to create new columns for the upper and lower
limits for the bars.
mutate()
functions similar to creating a new column in
Excel and putting an equation into the column that takes the difference
from mean_cover
.
It is unrealistic to teach all of the potential visualisations and
options in ggplot2
in a single lesson. So, we can suggest
course participants visit details as part of the tidyverse documentation,
including this cheatsheet.
There are also a number of extensions that add to
the core ggplot2
functionality that can be explored here.
Now that we have some basic skills to visualise data using
ggplot()
, we will now explore how to add complexity to the
graphics by using additional aesthetics and
faceting.