-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path210608_tidytuesday_great_lakes_fish.Rmd
88 lines (70 loc) · 1.97 KB
/
210608_tidytuesday_great_lakes_fish.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
title: "210504_tidytuesday_great_lakes_fish"
author: "Angela"
date: "8/6/2021"
output: html_document
---
```{r}
knitr::opts_chunk$set(echo = FALSE, messages = FALSE, warnings = FALSE)
```
```{r}
library(tidytuesdayR)
library(tidyverse)
library(plotly)
library(glue)
```
```{r}
# Read in the data from the tidy tuesday library.
tuesdata <- tidytuesdayR::tt_load('2021-06-08')
readme(tuesdata)
```
```{r}
fishing <- tuesdata$fishing
head(fishing)
```
```{r}
# Plotting function
sunburst_plotting_function <- function(lake_name){
# Selecting the rows that I want
sb_fish_data <- fishing %>%
filter(between(year, 2000, 2010),
lake == lake_name
) %>%
group_by(year, species) %>%
summarize(total = sum(grand_total, na.rm =TRUE)) %>%
arrange(-total)
# Wrangling the data into the lists that I need for plotly
fish_by_year <- sb_fish_data %>%
group_by(year) %>%
summarize(values = sum(total)) %>%
arrange(-year)
fish_by_species <- sb_fish_data %>%
group_by(year, species) %>%
summarize(values = sum(total)) %>%
# filter(values > 0) %>%
arrange(-year) %>%
mutate(id = paste(year, species))
fish_labels = c(glue('LAKE\n{toupper(lake_name)}'), fish_by_year$year, fish_by_species$species)
fish_ids = c(lake_name, fish_by_year$year, fish_by_species$id)
fish_parents = c("", replicate(length(fish_by_year$year), lake_name), fish_by_species$year)
fish_values = c(sum(fish_by_year$values), fish_by_year$values, fish_by_species$values)
# Sunburst plot
sunburst_plot <- plot_ly(
labels = fish_labels,
ids = fish_ids,
parents = fish_parents,
values = fish_values,
type = 'sunburst',
branchvalues = 'total',
sort = FALSE
) %>%
layout(font = list(family = "Droid Serif",
size = 16,
color = "black")
)
return(sunburst_plot)}
```
```{r}
all_plots <- lapply(unique(fishing$lake), function(x) sunburst_plotting_function(x))
all_plots
```