Lab 1

Author

Julia Lewis-Borgia

Part 0: The preamble

This is where you should prepare yourself for all of the work below. In other words, load all of the packages you might need to do the work you are doing below. It is generally a good idea to load data and packages at the top of an R document/script. Today we are just loading packages since reading in data is part of our tutorial.

#Loading ALL of my necessary packages here :)
library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.0      ✔ stringr 1.4.1 
✔ readr   2.1.2      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(ggplot2)

Part 1: Make an Rproject for this class.

Need help making a proj? Item number 3 in This tutorial can help.

Part 2: Ensure you working directory is set correctly!

a.) Set your working directory to work nicely with your rproj (you should have done this when you setup your proj). Show me what directory you chose using code in the chunk below

#My rproj working directory is: 
getwd()
[1] "/Users/julialewis-borgia/Documents/SDS300/r_labs/lab1"

b.) CHANGE your working directory using a line of code (the function you want here is setwd())

setwd("/Users/julialewis-borgia/Documents/SDS300/r_labs/lab1/testFolder")
setwd("/Users/julialewis-borgia/Documents/SDS300/r_labs/lab1")
getwd()
[1] "/Users/julialewis-borgia/Documents/SDS300/r_labs/lab1"

c.) CHANGE your working directory using the RStudio GUI. Set it back to your preferred working directory and show me that it worked using code

# I went Tools –> Global options –> General and then changed working directory there before changing it back
getwd()
[1] "/Users/julialewis-borgia/Documents/SDS300/r_labs/lab1"

Part 3: Read in data!

a.) Read in data from file (csv)

Go to the course moodle page and find the Labs section. Download the ‘reef_life_survey_habitat.csv’ file. Save it somewhere meaningful (your course rproj directory, perhaps?). Now, read it into R USING CODE (not the ‘import dataset’ GUI). Using read.csv() (base R) or read_csv (tidyverse) is strongly preferred. Have another method? Ask me first!

#Reading in data from a file, naming is, and preparing it as an R object for later use 
reef_life_survey <-read.csv('reef_life_survey_habitat.csv')
head(reef_life_survey)
        FID Key SiteCode                     Site SiteLat SiteLong   Country
1 M3_DATA.1   1     SI16 Eastern Bay Sth Solitary   -30.2   153.27 Australia
2 M3_DATA.2   2     SI16 Eastern Bay Sth Solitary   -30.2   153.27 Australia
3 M3_DATA.3   3     SI16 Eastern Bay Sth Solitary   -30.2   153.27 Australia
4 M3_DATA.4   4     SI16 Eastern Bay Sth Solitary   -30.2   153.27 Australia
5 M3_DATA.5   5     SI16 Eastern Bay Sth Solitary   -30.2   153.27 Australia
6 M3_DATA.6   6     SI16 Eastern Bay Sth Solitary   -30.2   153.27 Australia
      Ecoregion                 Realm SurveyID Depth           SurveyDate
1 Tweed-Moreton Temperate Australasia  2000974     9 2009-08-22T14:00:00Z
2 Tweed-Moreton Temperate Australasia  2000974     9 2009-08-22T14:00:00Z
3 Tweed-Moreton Temperate Australasia  2000974     9 2009-08-22T14:00:00Z
4 Tweed-Moreton Temperate Australasia  2000974     9 2009-08-22T14:00:00Z
5 Tweed-Moreton Temperate Australasia  2000974     9 2009-08-22T14:00:00Z
6 Tweed-Moreton Temperate Australasia  2000974     9 2009-08-22T14:00:00Z
  Category                 CategoryDescription MajorCategory NumPoints
1     Bare                           Bare Rock         Other         6
2      Ecr                   Encrusting corals         Coral         1
3     Sand                                Sand         Other         1
4     Bfol Small to medium foliose brown algae         Algae        11
5      CCA            Crustose coralline algae         Algae        14
6      FEA         Filamentous epiphytic algae         Algae         4
  TotalPoints PercentCover                 geom
1         100            6 POINT (153.27 -30.2)
2         100            1 POINT (153.27 -30.2)
3         100            1 POINT (153.27 -30.2)
4         100           11 POINT (153.27 -30.2)
5         100           14 POINT (153.27 -30.2)
6         100            4 POINT (153.27 -30.2)

b.) Read in data from a url (github, in this case)

Follow this link to my github page. Next, find the file ‘belize_coral_survey_data_2016.csv’ and read that file into R using the URL. Need help with this? Try looking here first, then ask me!

#Reading in some coral survey from Justin's github to practice with!
belize_2016 <- read.csv("https://raw.githubusercontent.com/jbaumann3/BIOL234_Biostats_MHC/main/belize_coral_survey_data_2016.csv")
head(belize_2016)
  method site      type lat transect diver    life.history species
1  Video    1 Back Reef   3        1     4 Stress Tolerant    SSID
2  Video    1 Back Reef   3        1     4            <NA>    MCOM
3  Video    1 Back Reef   3        1     4 Stress Tolerant    SSID
4  Video    1 Back Reef   3        1     4      Generalist    OFAV
5  Video    1 Back Reef   3        1     4           Weedy    UTEN
6  Video    1 Back Reef   3        1     4 Stress Tolerant    SSID
  percent.of.cover        l           w        area pale bleached total.pb
1             0.30  5.62500       5.625   31.640625    0        0        0
2             0.90 10.80000         5.4       58.32    0        0        0
3             0.80 29.41176 12.94117647 380.6228374    0        0        0
4             0.90 26.71875    18.28125 488.4521484    0        0        0
5             0.30 11.78571 16.07142857 189.4132654    0        0        0
6             0.85 21.25000 13.66071429 290.2901787    0        0        0
  percent.pb new trans old total.mort percent.mort disease percentdisease
1          0 0.7     0   0         16           70       0              0
2          0 0.0     0   0          2            0       0              0
3          0 0.0     0 0.4         10           40       0              0
4          0 0.0     0   0          2            0       0              0
5          0 0.0     0   0          2            0       0              0
6          0 0.1     0   0          4           10       0              0

5. Practice data manipulation

a) Using the data from my github, select columns of interest. This includes type, lat, life.history, species, and percent.of.cover (for now)

#select columns
belize_2016_partial <- select(belize_2016, type, lat, life.history, species, percent.of.cover)
head(belize_2016_partial)
       type lat    life.history species percent.of.cover
1 Back Reef   3 Stress Tolerant    SSID             0.30
2 Back Reef   3            <NA>    MCOM             0.90
3 Back Reef   3 Stress Tolerant    SSID             0.80
4 Back Reef   3      Generalist    OFAV             0.90
5 Back Reef   3           Weedy    UTEN             0.30
6 Back Reef   3 Stress Tolerant    SSID             0.85

b.) Filter that dataframe to include all life.history options except for NA

#Filter the data
belize_2016_partial <- belize_2016_partial %>% drop_na(life.history)
head(belize_2016_partial)
       type lat    life.history species percent.of.cover
1 Back Reef   3 Stress Tolerant    SSID             0.30
2 Back Reef   3 Stress Tolerant    SSID             0.80
3 Back Reef   3      Generalist    OFAV             0.90
4 Back Reef   3           Weedy    UTEN             0.30
5 Back Reef   3 Stress Tolerant    SSID             0.85
6 Back Reef   3      Generalist    OFAV             0.80

c.) Make a new column that contains a name for each latitude. Currently latitude (lat) is a number. Let’s make it a name that aligns with a coastal town, as this is easier to understand. Lat 1 is San Pedro, Lat 2 is Belize City, Lat 3 is Dangriga, Lat 4 is Placencia, Lat 5 is Punta Gorda. There are many ways to do this. Give it a shot! I’m happy to help.

#Add a new column that assigns a name to each latitude
belize_2016_partial <- belize_2016_partial %>% mutate(coastal_town = case_when(lat == 1 ~ "San Pedro", lat == 2 ~ "Belize City", lat == 3 ~ "Dangriga", lat == 4 ~ "Placencia", lat == 5 ~ "Punta Gorda")) 
head(belize_2016_partial)
       type lat    life.history species percent.of.cover coastal_town
1 Back Reef   3 Stress Tolerant    SSID             0.30     Dangriga
2 Back Reef   3 Stress Tolerant    SSID             0.80     Dangriga
3 Back Reef   3      Generalist    OFAV             0.90     Dangriga
4 Back Reef   3           Weedy    UTEN             0.30     Dangriga
5 Back Reef   3 Stress Tolerant    SSID             0.85     Dangriga
6 Back Reef   3      Generalist    OFAV             0.80     Dangriga

6.) Basic Plots

Make 3 exploratory plots in ggplot. You can manipulate data anyway you’d like. I strongly recommend avoiding bar graphs.

# plot 1
# find center and spread (box plot, 
# anova or regression would be used as a stats test on this data (multiple (more than two) categorical variables, one numerical variable)
boxplot <- ggplot(belize_2016, aes(x=type, y=percent.of.cover)) + 
  geom_boxplot(outlier.shape = NA) + 
  coord_cartesian(ylim = c(0, 1)) +
  labs(x = "Coral Reef Type", y = "Percent of Cover")
boxplot
Warning: Removed 2286 rows containing non-finite values (stat_boxplot).

# plot 2
boxplot2 <- ggplot(belize_2016, aes(x=life.history, y=percent.of.cover)) + 
  geom_boxplot(outlier.shape = NA) + 
  coord_cartesian(ylim = c(0, 1)) +
  labs(x = "Life History", y = "Percent of Cover")
boxplot2
Warning: Removed 2286 rows containing non-finite values (stat_boxplot).

# plot 3
scatterplot <- ggplot(belize_2016, aes(x=new, y=percent.of.cover)) +   geom_point(outlier.shape = NA) +
    coord_cartesian(ylim = c(0, 2)) +
    labs(x = "New", y = "Percent of Cover")
Warning: Ignoring unknown parameters: outlier.shape
scatterplot
Warning: Removed 2287 rows containing missing values (geom_point).

7.) save your dataframe to file

#save your dataframe to file
write.csv(belize_2016_partial,"C:\\Users\\julialewis-borgia\\Documents\\SDS300\\r_labs\\lab1", row.names=FALSE)

8.) Render and turn in your html quarto doc

You can do this however works for you. You should be able to turn in a link to your html file (if you publish on Rpubs or github, for exmaple) or you can turn in your actual html file. Ensure that is works though! IF I can’t load it you will get a U (and need to revise and resubmit)