Previous steps

If you would like to return to information from the previous section, please click here.

Context

The homework for the Visualisation of Status & Trends included a number of exercises, including:

  1. Creating “on-the-fly” data summaries
  2. Use of ggplot() geometries
  3. Faceting
  4. Figure export
  5. Managing workflow with integrate.R

Although there were a couple of example datasets available for course participants to work with (i.e. CPCe and Kenya fishes data), this wiki will illustrate an approach to the Homework using the regional WIO benthic data set.

Creating “on-the-fly” data summaries”

After loading the *.rda of the WIO regional benthic data from the data_intermediate folder, we are ready to begin with the homework exercises.

As the wio_regional_benthic_data object contains close to 110 thousand rows, including combinations of coutries, sectors, stations, benthic codes and replicate quadrates, in order to create clean easy to interpret visuals, we will need to summarise the data. This is easily accomplished by piping (i.e. %>%) the data to a group_by() statement and using summarise() for deriving mean() and sd() for country, year and benthic_code categories:

wio_regional_benthic_data %>%
       group_by(Country,
                # Station,
                Year,
                benthic_code,
                benthic_name) %>%
       summarise(pc_mean = percent_cover %>% mean(na.rm = TRUE),
                 pc_sd   = percent_cover %>%   sd(na.rm = TRUE))
`summarise()` has grouped output by 'Country', 'Year', 'benthic_code'. You can override using the `.groups` argument.
# A tibble: 1,576 x 6
# Groups:   Country, Year, benthic_code [1,576]
   Country  Year benthic_code benthic_name pc_mean pc_sd
   <chr>   <dbl> <chr>        <chr>          <dbl> <dbl>
 1 Comoros  1999 HC           Coral          0.540 0.186
 2 Comoros  2002 HC           Coral          0.441 0.158
 3 Comoros  2003 HC           Coral          0.478 0.175
 4 Comoros  2004 HC           Coral          0.442 0.200
 5 Comoros  2005 HC           Coral          0.536 0.216
 6 Comoros  2007 HC           Coral          0.610 0.233
 7 Comoros  2009 HC           Coral          0.520 0.255
 8 Comoros  2011 HC           Coral          0.233 0.136
 9 Comoros  2015 HC           Coral          0.646 0.186
10 Comoros  2016 HC           Coral          0.553 0.212
# … with 1,566 more rows

We can pipe this summary to ggplot() and set the aesthetic colour to Country to visualise trends of coral cover across the region:

    wio_regional_benthic_data %>%
      group_by(Country,
               # Station,
               Year,
               benthic_code,
               benthic_name) %>%
      summarise(pc_mean = percent_cover %>% mean(na.rm = TRUE),
                pc_sd   = percent_cover %>%   sd(na.rm = TRUE)) %>%
      mutate(sd_upper = pc_mean + pc_sd,
             sd_lower = pc_mean - pc_sd) %>%
      dplyr::filter(benthic_code %in% taxa_of_interest) %>%
    ggplot(aes(Year, pc_mean)) +
      geom_line(aes(colour = Country),
                alpha = 0.7) +
      geom_point(aes(colour = Country),
                 size  = 2.0,
                 alpha = 0.7) +
      geom_errorbar(aes(colour = Country,
                        ymax   = sd_upper,
                        ymin   = sd_lower)) +
      theme_bw() +
      scale_colour_manual(values = c_palette) +
      ylab("Mean Percent Cover") +
      xlab("") +
      ggtitle(paste0("WIO ", taxa_name)) +
      theme(strip.text.y = element_text(angle = 0),
            plot.title   = element_text(hjust = 0.5))=

Which looks something like this:

Similar to the Kenya fishes example, it is difficult to discern the patterns for individual countries. For this, we can use facets to better visualise the trends across the WIO region.

Using Facets

To the ggplot() code above, we can add facet_grid(Country ~ .) to align the WIO countries in rows. This allows us to compare across Year on the x-axis:

As individual countries are now in individual panels of the facet, we no longer need a legend (or even the colours!). We can remove these by removing the aesthetic and/or adding a line to the theme() for legend.position = "none"

Saving figures

For individual participants’ Homework, it could be advantageous to save the images in their personal figures folder. To do this, we can add a user_locale <- to assign to your personal folder. The rest follows our ggsave() convention:

 ## -- save for wiki -- ##
  # point to personal folder
    user_locale <- "participants_code/fsmith/"

  # point to save locale
    figure_locale <- "figures/examples/visualisation/"

  # save to file
    ggsave(paste0(user_locale,
                  figure_locale,
                  "wio_benthic_",
                  taxa_of_interest,
                  "by_country.png"),
      width  = 7,
      height = 7)

Next steps

Course participants who used the CPCe or Kenya fishes data might want to try these visualisations for the WIO regional benthic data set. One variation on this example would be to set the colour aesthetic to Station that would allow one to visualise the trends of individual sites within a Country. At the moment, the points and variation in the error bars represents the mean() and sd() at the Country level.

Aside from that, course participants should submit their Homework by pushing to Github using git add -A; git commit -m 'completing homework for visualisation'; git pull; git push.

We will build on these skills for the next module for spatial representation