Data Visualization

library(readr)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
library(tidyr)
library(forcats)

library(leaflet)
library(DT)
library(scales)

Attaching package: 'scales'
The following object is masked from 'package:readr':

    col_factor
library(janitor)

Attaching package: 'janitor'
The following objects are masked from 'package:stats':

    chisq.test, fisher.test
library(viridis)
Loading required package: viridisLite

Attaching package: 'viridis'
The following object is masked from 'package:scales':

    viridis_pal
delta_visits <- read_csv("https://portal.edirepository.org/nis/dataviewer?packageid=edi.587.1&entityid=cda8c1384af0089b506d51ad8507641f") %>% 
  janitor::clean_names() ## Introducing this new package!
Rows: 55 Columns: 13
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (4): EcoRestore_approximate_location, Reach, Time_of_Day, notes
dbl  (8): Latitude, Longitude, sm_boat, med_boat, lrg_boat, bank_angler, sci...
date (1): Date

ℹ 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.
glimpse(delta_visits)
Rows: 55
Columns: 13
$ eco_restore_approximate_location <chr> "Decker Island", "Decker Island", "De…
$ reach                            <chr> "Brannan to Decker Island", "Decker I…
$ latitude                         <dbl> 38.10587, 38.10587, 38.08456, 38.0845…
$ longitude                        <dbl> -121.7064, -121.7064, -121.7204, -121…
$ date                             <date> 2017-07-07, 2017-07-07, 2017-07-07, …
$ time_of_day                      <chr> "unknown", "unknown", "unknown", "unk…
$ sm_boat                          <dbl> 0, 0, 0, 0, 2, 0, 0, 7, 1, 0, 0, 0, 0…
$ med_boat                         <dbl> 2, 4, 0, 1, 10, 0, 0, 1, 2, 0, 1, 6, …
$ lrg_boat                         <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0…
$ bank_angler                      <dbl> 1, 3, 0, 0, 0, 0, 0, 0, 2, 0, 0, 5, 0…
$ scientist                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ cars                             <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ notes                            <chr> "no notes", "no notes", "Nobody or tr…
range(delta_visits$date)
[1] "2017-07-07" "2018-03-13"
unique(delta_visits$time_of_day)
[1] "unknown" "morning"
dput(colnames(delta_visits))
c("eco_restore_approximate_location", "reach", "latitude", "longitude", 
"date", "time_of_day", "sm_boat", "med_boat", "lrg_boat", "bank_angler", 
"scientist", "cars", "notes")
visits_long <- delta_visits %>% 
  pivot_longer(cols = c("sm_boat", "med_boat", "lrg_boat", "bank_angler", "scientist", "cars"), 
               names_to = "visitor_type", values_to = "quantity") %>% 
  rename(restore_loc = eco_restore_approximate_location) %>% 
  select(-notes)
# ggplot(visits_long, aes(x = reorder(visitor_type, -quantity), y = quantity)) + 
#          geom_bar(stat = "identity") + theme_bw()

ggplot(visits_long, aes(x = reorder(restore_loc, -quantity), y = quantity)) + 
         geom_bar(stat = "identity") + theme_bw() + theme(axis.text.x = element_text(angle = 45, hjust = 1))

Calculate daily visits by visit type

daily_visit <- visits_long %>% group_by(restore_loc, date, visitor_type) %>% 
  summarise(daily_visits = sum(quantity))
`summarise()` has grouped output by 'restore_loc', 'date'. You can override
using the `.groups` argument.
ggplot(daily_visit, aes(x = reorder(restore_loc, -daily_visits), y = daily_visits)) + 
         geom_col(stat = "identity") + theme_bw() + theme(axis.text.x = element_text(angle = 45, hjust = 1))
Warning in geom_col(stat = "identity"): Ignoring unknown parameters: `stat`

Customizing our plot

ggplot(daily_visit, aes(x = reorder(restore_loc, -daily_visits), y = daily_visits)) + 
         geom_col(stat = "identity", fill = "aquamarine4") + theme_bw() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
Warning in geom_col(stat = "identity", fill = "aquamarine4"): Ignoring unknown
parameters: `stat`

ggplot(daily_visit, aes(x = restore_loc, y = daily_visits, fill = visitor_type)) +
  geom_col() +
  labs(x = "Restoration Locations", y = "Number of Visits",
       fill = "Type of visitor", Title = "Total number of visits to Delta Restoration sites by visitor type",
       subtitle = "Sum of all visits during study period") +
  coord_flip() + theme_classic()

using ’theme() function to further customize our plot

ggplot(daily_visit, aes(x = restore_loc, y = daily_visits, fill = visitor_type)) +
  geom_col() +
  labs(x = "Restoration Locations", y = "Number of Visits",
       fill = "Type of visitor", Title = "Total number of visits to Delta Restoration sites by visitor type",
       subtitle = "Sum of all visits during study period") +
  coord_flip() + theme_bw() +
  theme(legend.position = "bottom", axis.ticks.y = element_blank())

Saving theme into an object

my_theme <- theme_bw(base_size = 16) + theme(legend.position = "bottom", axis.ticks.y = element_blank())
ggplot(daily_visit, aes(x = restore_loc, y = daily_visits, fill = visitor_type)) +
  geom_col() +
  labs(x = "Restoration Locations", y = "Number of Visits",
       fill = "Type of visitor", Title = "Total number of visits to Delta Restoration sites by visitor type",
       subtitle = "Sum of all visits during study period") +
  coord_flip() + my_theme + scale_y_continuous(breaks = seq(0,120, 20))

daily_visit_total <- daily_visit %>% group_by(restore_loc) %>% mutate(n = sum(daily_visits)) %>% ungroup()

ggplot(daily_visit_total, aes(x = reorder(restore_loc, n), y = daily_visits, fill = visitor_type)) +
  geom_col() +
  labs(x = "Restoration Locations", y = "Number of Visits",
       fill = "Type of visitor", Title = "Total number of visits to Delta Restoration sites by visitor type",
       subtitle = "Sum of all visits during study period") +
  coord_flip() + my_theme + scale_y_continuous(breaks = seq(0,120, 20)) +
  scale_fill_viridis_d(option = "C")

ggsave("figures/visit_restore_sites_delta.jpg", width = 12, height = 6, units = "in")

Creating multiple plots with ’facet_wrap()

ggplot(daily_visit_total, aes(x = visitor_type, y = daily_visits, fill = visitor_type)) +
  geom_col() +
  labs(x = "Restoration Locations", y = "Number of Visits",
       fill = "Type of visitor", Title = "Total number of visits to Delta Restoration sites by visitor type",
       subtitle = "Sum of all visits during study period") + theme_bw(base_size = 16) +
  theme(legend.position = "bottom", axis.ticks.x = element_blank(), axis.text.x = element_blank()) +
  scale_y_continuous(breaks = seq(0,120, 20)) + 
  scale_fill_viridis_d(option = "C") + facet_wrap(~restore_loc, nrow = 2)

##Interactive tables with ‘DT’

locations <- visits_long %>% distinct(restore_loc, .keep_all = T) %>% select(restore_loc, latitude, longitude)
datatable(locations)

interactive map with ’Leaflet

leaflet(locations) %>% addTiles() %>% addMarkers(~longitude, ~latitude, popup = ~restore_loc)