Attaching package: 'lubridate'
The following objects are masked from 'package:base':
date, intersect, setdiff, union
library(ggsci)library(patchwork)
Essentials:
1.) Read in https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-05-04/water.csv check the format of the date column and change it using lubridate so it is correct
#read in the water datawater <-read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-05-04/water.csv')
Rows: 473293 Columns: 13
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (9): report_date, status_id, water_source, water_tech, facility_type, co...
dbl (4): row_id, lat_deg, lon_deg, install_year
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
str(water)
spc_tbl_ [473,293 × 13] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ row_id : num [1:473293] 3957 33512 35125 37760 38118 ...
$ lat_deg : num [1:473293] 8.073 7.374 0.773 0.781 0.779 ...
$ lon_deg : num [1:473293] 38.6 40.5 34.9 35 35 ...
$ report_date : chr [1:473293] "04/06/2017" "08/04/2020" "03/18/2015" "03/18/2015" ...
$ status_id : chr [1:473293] "y" "y" "y" "y" ...
$ water_source : chr [1:473293] NA "Protected Spring" "Protected Shallow Well" "Borehole" ...
$ water_tech : chr [1:473293] NA NA NA NA ...
$ facility_type: chr [1:473293] NA "Improved" "Improved" "Improved" ...
$ country_name : chr [1:473293] "Ethiopia" "Ethiopia" "Kenya" "Kenya" ...
$ install_year : num [1:473293] NA 2019 NA NA NA ...
$ installer : chr [1:473293] "Private-CRS" "WaterAid" NA NA ...
$ pay : chr [1:473293] NA NA NA NA ...
$ status : chr [1:473293] NA NA NA NA ...
- attr(*, "spec")=
.. cols(
.. row_id = col_double(),
.. lat_deg = col_double(),
.. lon_deg = col_double(),
.. report_date = col_character(),
.. status_id = col_character(),
.. water_source = col_character(),
.. water_tech = col_character(),
.. facility_type = col_character(),
.. country_name = col_character(),
.. install_year = col_double(),
.. installer = col_character(),
.. pay = col_character(),
.. status = col_character()
.. )
- attr(*, "problems")=<externalptr>
site type lat transect diver cc_percent
1 1 Back Reef 3 1 4 5.8435758
2 1 Back Reef 3 2 4 0.9505263
3 1 Back Reef 3 3 4 5.2423389
4 1 Back Reef 3 4 5 5.0040475
5 1 Back Reef 3 5 5 5.8954916
6 2 Patch Reef 3 1 4 5.2826190
3 Look at the data and generate a hypothesis to test (X, Y, and/or Z has no effect on coral cover (cc_percent), for example). cc_percent is the only numerical variable we care about here! Everything else is categorical. Write your hypothesis in BOLD below.
\(\mathbf{H_0}:\)Reef type is not associated with mean coral cover (cc_percent)
\(\mathbf{H_A}:\)Reef type is associated with the mean amount of coral cover
4 Filter our the columns that you are not using for your hypothesis test
type cc_percent
1 Back Reef 5.8435758
2 Back Reef 0.9505263
3 Back Reef 5.2423389
4 Back Reef 5.0040475
5 Back Reef 5.8954916
6 Patch Reef 5.2826190
5 Using the pipe, %>%, group your data and calcualte the mean(s) you need for your visual hypothesis test
bzcoral3 <- bzcoral2 %>%group_by(type) %>%summarize(mean_cc =mean(cc_percent), sd =sd(cc_percent), n =n(), se = sd/sqrt(n))head(bzcoral3)
# A tibble: 3 × 5
type mean_cc sd n se
<chr> <dbl> <dbl> <int> <dbl>
1 Back Reef 11.9 9.52 29 1.77
2 Nearshore 3.16 2.25 18 0.531
3 Patch Reef 12.7 11.5 30 2.11
6 Graph your results! Means + errorbars required :) Make a nice, easy to see graph with clear labels and text
ggplot(bzcoral3, aes(x = type, y = mean_cc, color = type)) +geom_point() +geom_jitter(data = bzcoral2, aes(x = type, y = cc_percent, color = type), alpha =0.3, size =0.3) +geom_errorbar(data = bzcoral3, aes(x = type, ymin = mean_cc-se, ymax = mean_cc+se), width =0.2) +scale_color_lancet() +theme_classic() +labs(x ="Reef Type", y ="Mean Coral Cover", title ="Mean Coral Cover by Reef Type") +theme(plot.title =element_text(hjust =0.5))
7 Assess your hypothesis! What does your graph show (Note: We did not do stats, so please do not say ‘significant’)
By looking at the graph, we can probably reject the null hypothesis that there is no association between reef type and mean coral cover. (The mean coral cover of Nearshore reefs looks to be smaller than the mean coral cover of Back Reef and Patch Reef.)
Depth
1: Read in intertidal transect data. View it, identify the columns that contain species/cover items and pivot from wide to long format!
2 Filter data such that we retain only ‘species’ that are animals. These include: Carcinus, Cancer Crabs, nucella, litt_obt, litt_lit, litt_sax, semibal. Everything else is either rock or algae. Please do this filter step AFTER you pivot to long format. Note: It is easier to do this when you are in wide format, but this is good practice! Ask questions if you need help :). Hint: The ‘|’ key is how you say ‘or’ in code :)
op_animals <- long_op %>%filter(species =="Cancer.crabs"| species =="carcinus"| species =="litt_lit"| species =="litt_obt"| species =="litt_sax"| species =="semibal"| species =="nucella")head(op_animals)
2 Using the same data–> rename the factors in the trans_description column based on wave exposure. a_cobble_protected is low, c_flat_profile and d_semi_expos are both moderate, e_exposed is high. Hint: use ifelse(). Justin can help!
3 Using the same data, make a simplified tidal height column. We want tidal height cat <4 to be ‘low’, >7 to be m’oderate’high’, and in between to be ‘intermediate’. Hint: You can use if_else for this as well!
4 Using the same dataframe, build 2 new dataframes, one calculating mean and error (standard error) for semibal (barnacle) abundance and one doing the same for nucella (whelk) abundance by tidal height and wave exposure (trans_description)
semibal <- op_animals %>%filter(species =="semibal") %>%group_by(wave, tidal_ht) %>%summarise(mean =mean(abundance), sd =sd(abundance), n =n(), se = sd/sqrt(n))
`summarise()` has grouped output by 'wave'. You can override using the
`.groups` argument.
semibal$species ="Semibal"head(semibal)
# A tibble: 6 × 7
# Groups: wave [2]
wave tidal_ht mean sd n se species
<chr> <chr> <dbl> <dbl> <int> <dbl> <chr>
1 High High 0 0 12 0 Semibal
2 High Intermediate 32.4 30.4 18 7.17 Semibal
3 High Low 6.11 7.82 9 2.61 Semibal
4 Low High 0 0 9 0 Semibal
5 Low Intermediate 2.24 5.24 17 1.27 Semibal
6 Low Low 6.11 9.53 9 3.18 Semibal
nucella <- op_animals %>%filter(species =="nucella") %>%group_by(wave, tidal_ht) %>%summarise(mean =mean(abundance), sd =sd(abundance), n =n(), se = sd/sqrt(n))
`summarise()` has grouped output by 'wave'. You can override using the
`.groups` argument.
nucella$species ="Nucella"head(nucella)
# A tibble: 6 × 7
# Groups: wave [2]
wave tidal_ht mean sd n se species
<chr> <chr> <dbl> <dbl> <int> <dbl> <chr>
1 High High 0 0 12 0 Nucella
2 High Intermediate 0 0 18 0 Nucella
3 High Low 1.78 5.33 9 1.78 Nucella
4 Low High 0 0 9 0 Nucella
5 Low Intermediate 0 0 17 0 Nucella
6 Low Low 0 0 9 0 Nucella
5 Plot barnacle and snail abundance + error across tidal heights & wave exposures. You can plot them both on the same graphs (merging your dataframes could help) or you can make multiple graphs and patchwork them together.
combined <-rbind(semibal, nucella)ggplot(combined, aes(x = wave, y = mean, color = tidal_ht)) +facet_wrap(~species) +geom_point() +geom_errorbar(data = combined, aes(x = wave, ymin = mean-se, ymax = mean+se), width =0.2) +scale_color_lancet() +theme_classic() +labs(y ="Count", x ="Wave exposure", color ="Tidal height")
6 Write a short interpretative statement. We didn’t run any stats, so avoid the word ‘significant.’ How do barnacles and whelk abundances appear to vary by tidal height and wave exposure? I do not need you to tell me anything about the ecology of the system, but feel free to do so if you’d like :) I just need you to interpret your graphs.
It looks like neither Nucella (welk) nor Semibal (barnacles) are found at high tidal heights. It appears that Semibal are most abundant where there is high wave exposure and intermediate tidal height. Looking at these graphs, the data shows that Nucella are found when there is both moderate wave exposure and intermediate or low tidal height or high wave exposure and low tidal height.