1. 
  2. # live coding script ------------------------------------------------------
  3. 
  4. # Website: https://tbep-tech.github.io/tbep-r-training/index.html
  5. # Live Coding Link: https://tinyurl.com/tbep-r-training-live-coding
  6. # Data: https://tbep-tech.github.io/tbep-r-training/data/data.zip
  7. # R scripts by lesson: https://tbep-tech.github.io/tbep-r-training/scripts.zip
  8. 
  9. # R basics ----------------------------------------------------------------
  10. 
  11. 1 + 1
  12. 
  13. # here's something that prints text
  14. print('hello world')
  15. 
  16. seq(1, 10)
  17. 
  18. rnorm(100, mean = 10, sd = 2)
  19. 
  20. mean(rnorm(100))
  21. 
  22. mysum <- sum(rnorm(100))
  23. 
  24. # load libraries
  25. library(tidyverse)
  26. library(sf)
  27. library(mapview)
  28. 
  29. fishdat <- read_csv('data/fishdat.csv')
  30. statloc <- read_csv('data/statloc.csv')
  31. 
  32. # data wrangling 1 --------------------------------------------------------
  33. 
  34. library(tidyverse)
  35. 
  36. fishdat <- read_csv('data/fishdat.csv')
  37. 
  38. dim(fishdat)
  39. names(fishdat)
  40. 
  41. head(fishdat)
  42. View(fishdat)
  43. 
  44. dplyr_sel1 <- select(fishdat, Sampling_Date, Gear, Pinfish)
  45. dplyr_sel3 <- select(fishdat, matches('c'))
  46. 
  47. dplyr_high_catch <- filter(fishdat, Pinfish > 30)
  48. 
  49. dplyr_gear20 <- filter(fishdat, Gear == 20)
  50. 
  51. filt1 <- filter(fishdat, Pinfish > 30 & Pinfish < 100)
  52. 
  53. dplyr_mut <- mutate(fishdat, bluemutate = Bluefish / 100)
  54. 
  55. dplyr_mut2 <- mutate(fishdat, mullet_cat = ifelse(Mullets < 3, 'few', 'many'))
  56. 
  57. dplyr_mut3 <- mutate(fishdat, newcol = 1)
  58. 
  59. dplyr_arr <- arrange(fishdat, `Sand Seatrout`)
  60. 
  61. dplyr_rnm <- rename(fishdat, snook = `Common Snook`)
  62. 
  63. dplyr_rnm <- rename(fishdat, snook = Bluefish)
  64. 
  65. # ex1
  66. ex1 <- select(fishdat, Reference, Sampling_Date, Gear, `Sand Seatrout`)
  67. ex1 <- filter(ex1, Gear == 20)
  68. head(ex1)
  69. ex1 <- rename(ex1, date = Sampling_Date)
  70. head(ex1)
  71. nrow(ex1)
  72. 
  73. # not using pipes, select a column, filter rows
  74. bad_ex <- select(fishdat, Gear, `Common Snook`)
  75. bad_ex2 <- filter(bad_ex, `Common Snook` > 25)
  76. View(bad_ex2)
  77. 
  78. # with pipes, select a column, filter rows
  79. good_ex <- fishdat %>%
  80. select(Gear, `Common Snook`) %>%
  81. filter(`Common Snook` > 25)
  82. 
  83. # examples
  84. ex4 <- select(fishdat, Reference, Sampling_Date, Gear, `Sand Seatrout`)
  85. ex4 <- filter(ex4, Gear == 20)
  86. ex4 <- rename(ex4, date = Sampling_Date)
  87. 
  88. ex5 <- fishdat %>%
  89. select(Reference, Sampling_Date, Gear, `Sand Seatrout`) %>%
  90. filter(Gear == 20) %>%
  91. rename(date = Sampling_Date)
  92. 
  93. identical(ex5, ex1)
  94. 
  95. # data wrangling 2 --------------------------------------------------------
  96. 
  97. library(tidyverse)
  98. 
  99. # load the fish data
  100. fishdat <- read_csv('data/fishdat.csv')
  101. 
  102. # load the station data
  103. statloc <- read_csv('data/statloc.csv')
  104. 
  105. head(fishdat)
  106. head(statloc)
  107. 
  108. # join the two
  109. joindat <- left_join(fishdat, statloc, by = 'Reference')
  110. 
  111. View(joindat)
  112. 
  113. # exercise 6
  114. ex6 <- fishdat %>%
  115. select(Reference, Sampling_Date, Gear, `Common Snook`) %>%
  116. filter(Gear == 20)
  117. 
  118. jnex6 <- full_join(ex6, statloc, by = 'Reference')
  119. 
  120. dim(ex6)
  121. nrow(ex6)
  122. 
  123. gather(table4a, `1999`, `2000`, key = "year", value = "cases")
  124. 
  125. sprd_ex <- spread(table2, key = type, value = count)
  126. 
  127. # check dimensions, structure
  128. dim(fishdat)
  129. str(fishdat)
  130. 
  131. # gather the fishdat
  132. gatherdat <- fishdat %>%
  133. gather(key = 'Species', value = 'Count', Bluefish, `Common Snook`, Mullets, Pinfish, `Red Drum`, `Sand Seatrout`)
  134. 
  135. 
  136. by_spp <- group_by(gatherdat, Species) %>%
  137. summarise(avecnt = mean(Count))
  138. 
  139. # multiple summaries
  140. # multiple groups
  141. more_sums <-gatherdat %>%
  142. group_by(yr, Species) %>%
  143. summarize(
  144. n = n(),
  145. min_count = min(Count),
  146. max_count = max(Count),
  147. avecnt = mean(Count)
  148. )
  149. 
  150. # ex8
  151. 
  152. # exercise 8
  153. ex8 <- gatherdat %>%
  154. filter(Gear == 20 & Species == 'Pinfish') %>%
  155. group_by(Reference) %>%
  156. summarise(avecnt = mean(Count)) %>%
  157. arrange(-avecnt)
  158. 
  159. 
  160. # data viz and graphics ---------------------------------------------------
  161. 
  162. library(tidyverse)
  163. 
  164. plot(hwy ~ displ, data = mpg)
  165. ?plot
  166. 
  167. plot(mpg$displ, mpg$hwy)
  168. 
  169. plot(hwy ~ displ, data = mpg, main = 'Highway mileage by engine size',
  170. xlab = 'engine size (l)', ylab = 'highway (mpg)')
  171. 
  172. plot(hwy ~ displ, data = mpg, main = 'Highway mileage by engine size',
  173. xlab = 'engine size (l)', ylab = 'highway (mpg)',
  174. col = 'blue', pch = 18, cex = 0.75, family = 'serif')
  175. 
  176. hist(mpg$cty)
  177. 
  178. fishdat <- read_csv('data/fishdat.csv')
  179. 
  180. plot(Pinfish ~ Sampling_Date, data = fishdat, main = "Catch over time")
  181. 
  182. ggplot(data = mpg) +
  183. geom_point(mapping = aes(x = displ, y = hwy))
  184. 
  185. ggplot(mpg, aes(x = displ, y = hwy)) +
  186. geom_linerange()
  187. ?geom_linerange
  188. 
  189. ggplot(mpg, aes(x = displ, y = hwy, colour = drv, shape = drv)) +
  190. geom_point()
  191. 
  192. ggplot(data = fishdat) +
  193. geom_point(mapping = aes(x = Sampling_Date, y = Pinfish)) +
  194. scale_y_log10() +
  195. theme_bw()
  196. 
  197. 
  198. ggplot(data = fishdat) +
  199. geom_point(mapping = aes(x = Sampling_Date, y = Pinfish)) +
  200. scale_y_log10() +
  201. theme_minimal()
  202. 
  203. 
  204. ggplot(data = fishdat) +
  205. geom_point(mapping = aes(x = Sampling_Date, y = Pinfish)) +
  206. scale_y_log10() +
  207. theme_grey()
  208. 
  209. install.package('ggthemes')
  210. 
  211. ggplot(data = fishdat) +
  212. geom_point(mapping = aes(x = Sampling_Date, y = Pinfish)) +
  213. scale_y_log10() +
  214. theme_grey(base_family = 'serif', base_size = 16)
  215. 
  216. ggplot(mpg, aes(x = displ, y = hwy, colour = drv, shape = drv)) +
  217. geom_point() +
  218. stat_smooth()
  219. 
  220. ggplot(mpg, aes(x = displ, y = hwy)) +
  221. geom_point(aes(colour = drv)) +
  222. stat_smooth()
  223. 
  224. ggplot(mpg, aes(x = displ, y = hwy, colour = drv)) +
  225. geom_point() +
  226. stat_smooth(method = 'lm') +
  227. facet_wrap(~ drv, ncol = 2)
  228. 
  229. ggplot(data = fishdat, mapping = aes(x = Sampling_Date, y = Pinfish)) +
  230. geom_point() +
  231. scale_y_log10() +
  232. theme_minimal() +
  233. stat_smooth(method = 'lm') +
  234. facet_wrap(~Gear, ncol = 3)
  235. 
  236. 
  237. # spatial analysis --------------------------------------------------------
  238. 
  239. library(tidyverse)
  240. library(sf)
  241. library(mapview)
  242. 
  243. fishdat <- read_csv('data/fishdat.csv')
  244. statloc <- read_csv('data/statloc.csv')
  245. sgdat <- st_read('data/sgdat.shp')
  246. 
  247. class(sf)
  248. 
  249. "+proj=longlat +datum=WGS84"
  250. 
  251. # ex11
  252. 
  253. # data
  254. alldat <- left_join(fishdat, statloc, by = 'Reference')
  255. 
  256. # create spatial data object
  257. alldat <- st_as_sf(alldat, coords = c('Longitude', 'Latitude'), crs = "+proj=longlat +datum=WGS84")
  258. 
  259. st_crs(alldat)
  260. 
  261. # create spatial data object
  262. alldat <- st_as_sf(alldat, coords = c('Longitude', 'Latitude'), crs = st_crs(sgdat))
  263. 
  264. plot(st_geometry(alldat))
  265. 
  266. filt_dat <- alldat %>%
  267. filter(yr == 2016)
  268. plot(st_geometry(filt_dat))
  269. 
  270. fish_crop <- filt_dat[sgdat, ]
  271. plot(st_geometry(fish_crop))
  272. 
  273. fish_int <- st_intersection(filt_dat, sgdat)
  274. plot(st_geometry(fish_int))
  275. 
  276. # use ggplot with sf objects
  277. ggplot() +
  278. geom_sf(data = sgdat, aes(fill = FLUCCS)) +
  279. geom_sf(data = fish_int)
  280. 
  281. mapview(sgdat, col.regions = 'green') +
  282. mapview(fish_int, zcol = 'Gear')