Interactive

Interactive charts and tables offer many advantages over static representations of data. Interactive charts and tables enable exploration of data in a more engaging way, charts can be zoomed (in and out), tables filtered and data points compared. This makes it easier to find patterns and trends, and to identify outliers. There use should lead to more accurate conclusions and insights.

1 Maps

1.1 Lydford School Transect

Code
library(tidyverse)
library(leaflet)

# Pallet of colours for bat species on Leaflet map
# Leaflet  bat colours
LeafColours<- c("#ffff99",
                  "#b15928",
                  "#1f78b4",
                  "#A52A2A",
                  "#7FFF00",
                  "#b2df8a",
                  "#6a3d9a",
                  "#ff7f00",
                  "#a6cee3",
                  "#BCEE68",
                  "#8B0000",
                  "#8B0000",
                  "#000000",
                  "#8A2BE2",
                  "#fdbf6f",
                  "#e31a1c",
                  "#33a02c",
                  "#cab2d6",
                  "#fb9a99",
                  "#EEE8CD",
                  "#008B8B",
                  "#000000")

# Species list for colours
Species <- c("Pipistrellus pipistrellus",
             "Pipistrellus pygmaeus",
             "Barbastella barbastellus",
             "Myotis alcathoe",
             "Myotis bechsteinii",
             "Myotis brandtii",
             "Myotis mystacinus",
             "Myotis nattereri",
             "Myotis daubentonii",
             "Myotis spp.",
             "Plecotus auritus",
             "Plecotus spp.",
             "Plecotus austriacus",
             "Pipistrellus nathusii",
             "Pipistrellus spp.",
             "Rhinolophus ferrumequinum",
             "Rhinolophus hipposideros",
             "Nyctalus noctula",
             "Nyctalus leisleri",
             "Nyctalus spp.",
             "Eptesicus serotinus",
             "Pipistrelle Social")

ColourVector <- tibble(Species, LeafColours)

ColourVector <- ColourVector %>% 
  arrange(Species)

pal <- colorFactor(ColourVector$LeafColours, domain = ColourVector$Species)

Lydford %>%
    mutate(Popup_text = stringr::str_c(Common, # Make popup text string
                                       " ",
                                       as.character(round(post_set_min, 0)), 
                                       " minutes after sunset")) %>% 
    leaflet() %>% 
    addTiles() %>% 
    addCircleMarkers(
      lng = ~Longitude, lat = ~Latitude,
      radius = 7,
      color = ~pal(Species),
      stroke = TRUE, fillOpacity = 0.7,
      popup = ~Popup_text, label = ~Popup_text
    )
Figure 1: Bat Observations From Lydford School’s Bat Walk

Figure 1 shows Lydford Primary School’s bat walk observations from Wednesday evening 18th September 2019. The walk, between the School and the River Lyd, on the western edge of Dartmoor, started at sunset 6:27pm and continued for just over an hour. The observations were made with a Batlogger M bat detector that records the sound of the bat as it passes together with the time and postion; using a built-in Global Positioning System (GPS). These sounds where then analysed with software designed to help determine which species made the echolocation; BatExplorer was the software used.

Figure 1 is an interactive map; hovering the mouse over the coloured point will show the species and the time, in minutes, after sunset the bat was observed.

The data shown in Figure 1, Lydford is available from the iBats package.

2 Graphs

2.1 Making ggplot Interactive

Plotly1 allows easy translation of ggplot2 graphics into an interactive web-based version. Hovering the mouse over a graph point will reveal the bat species, the time bat was recorded and the length in seconds of the activity. The graph can be copied to the clipboard plus zoomed and panned; the home symbol will reset the axes. The corresponding bubble chart for the TavyOak data produced entirely in Plotly is shown in Figure 6.

Code
library(tidyverse)
library(iBats)
library(plotly)

# graph anotation
graph_sunrise <- TavyOak$sunrise[1]
graph_sunset <- TavyOak$sunset[1]

# graph time limits x-axis
graph_limit1 <- TavyOak$sunset[1] - lubridate::hours(1)
graph_limit2 <- TavyOak$sunrise[1] + lubridate::hours(1)

# colour values used by scale_fill_manual()
graph_bat_colours <- iBats::bat_colours_default(TavyOak$Species)

p <- TavyOak %>% 
  rename(`Date & Time` = DateTime,
         `Bat species` = Species,
         `Activity time (secs)` = bat_time) %>% 
  ggplot(aes(x = `Date & Time`, 
                      y = 1, 
                      fill = `Bat species`, 
                      size = `Activity time (secs)`)) +
  geom_jitter(shape = 21, alpha = 0.7) +
  scale_fill_manual(values = graph_bat_colours) +
  scale_size_area(max_size = 12) +
  scale_x_datetime(
    date_labels = "%H:%M hrs",
    date_breaks = "1 hour",
    limits = c(graph_limit1, graph_limit2)
  ) +
  labs(
    title = "One Night's Bat Activity \nAt a West Devon Oak Tree - June 2018",
    y = "For clarity activity is spread across the verstical scale"
  ) +
  theme_bw() +
  theme(
    plot.margin = margin(1, 1, 1, 1, "cm"),
    legend.position = "none",
    panel.grid.major.x = element_line(),
    panel.grid.major.y = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "midnightblue", colour = "black"),
    axis.text.x = element_text(size = 12, angle = 45, colour = "white"),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_text(size = 12, colour = "white"),
    plot.title = element_text(size=16, colour = "white")
  )

# Make `ggplot` interactive
ggplotly(p) 
Figure 2: Time Bats Were Active Through The Night

2.2 Bar Chart

Figure 3 shows a bar chart of monthly activity, it was drawn with the plotly library2. Hovering the mouse over a bar will reveal the bat species and the number of bat passes; toggling the species name in the legend will remove (or add) the species bar from the chart. At any view the graph can be downloaded as a .png file.

Code
library(tidyverse)
library(iBats)
library(plotly)

# Annex II Species of Bat in the UK
AnnexII <- c("Barbastella barbastellus", 
             "Rhinolophus ferrumequinum", 
             "Rhinolophus hipposideros", 
             "Myotis bechsteinii")

# Add data and time information to the iBats statics bat survey data set using the iBats::date_time_info
statics_plus <- iBats::date_time_info(statics) %>% 
   filter(Species %in% AnnexII)

statics_plus %>% 
  count(MonthFull, Species) %>% 
  plot_ly(x = ~MonthFull, y = ~n, color = ~Species,
          hoverinfo = "text",
          text = ~paste("Species:", Species, "<br>",
                        "Count:", n)) %>%
  add_bars(colors = c("#1f78b4", "#e31a1c", "#33a02c")) %>%
  layout(barmode = "stack",
         xaxis = list(title=""),
         yaxis = list(title="Count of Bat Passes (Nr)"))
Figure 3: Monthly Activity of Annex II Species

Figure 4 shows a bar chart of species activity relative to the static location, it was drawn with the plotly library3. Hovering the mouse over a bar will reveal the bat species and the number of bat passes; toggling the species name in the legend will remove (or add) the species bar from the chart. At any view the graph can be downloaded as a .png file.

Code
library(tidyverse)
library(iBats)
library(plotly)

# bat species colours
bat_colours_sci <- c(
  "Barbastella barbastellus" = "#1f78b4",
  "Myotis alcathoe" = "#a52a2a",
  "Myotis bechsteinii" = "#7fff00",
  "Myotis brandtii" = "#b2df8a",
  "Myotis mystacinus" = "#6a3d9a",
  "Myotis nattereri" = "#ff7f00",
  "Myotis daubentonii" = "#a6cee3",
  "Myotis spp." = "#bcee68",
  "Plecotus auritus" = "#8b0000",
  "Plecotus spp." = "#8b0000",
  "Plecotus austriacus" = "#000000",
  "Pipistrellus pipistrellus" = "#ffff99",
  "Pipistrellus nathusii" = "#8a2be2",
  "Pipistrellus pygmaeus" = "#b15928",
  "Pipistrellus spp." = "#fdbf6f",
  "Rhinolophus ferrumequinum" = "#e31a1c",
  "Rhinolophus hipposideros" = "#33a02c",
  "Nyctalus noctula" = "#cab2d6",
  "Nyctalus leisleri" = "#fb9a99",
  "Nyctalus spp." = "#eee8cd",
  "Eptesicus serotinus" = "#008b8b"
)
location_count <- statics %>% 
  count(Description, Species) 

list_species <- levels(factor(location_count$Species))

# get a vector of colours from the bat_pass_counts data.frame
colors <- unname(bat_colours_sci[list_species])


  plot_ly(location_count, y = ~Description, x = ~n, color = ~Species,
          orientation = 'h') %>%
  add_bars(colors = colors) %>%
  layout(barmode = "stack",
         yaxis = list(title=""),
         xaxis = list(title="Count of Bat Passes (Nr)"))
Figure 4: Species Activity for Each Static Location

2.3 Pie Chart

Figure 5 shows a pie chart of bat passes, it was drawn with the plotly library4. Hovering the mouse over a bar will reveal the bat species and the number of bat passes; toggling the species name in the legend will remove (or add) the species slice from the chart. At any view the graph can be downloaded as a .png file.

Code
library(tidyverse)
library(iBats)
library(plotly)

# Add data and time information to the iBats statics bat survey data set using the iBats::date_time_info
statics_plus <- iBats::date_time_info(statics)

# bat species colours
bat_colours_sci <- c(
  "Barbastella barbastellus" = "#1f78b4",
  "Myotis alcathoe" = "#a52a2a",
  "Myotis bechsteinii" = "#7fff00",
  "Myotis brandtii" = "#b2df8a",
  "Myotis mystacinus" = "#6a3d9a",
  "Myotis nattereri" = "#ff7f00",
  "Myotis daubentonii" = "#a6cee3",
  "Myotis spp." = "#bcee68",
  "Plecotus auritus" = "#8b0000",
  "Plecotus spp." = "#8b0000",
  "Plecotus austriacus" = "#000000",
  "Pipistrellus pipistrellus" = "#ffff99",
  "Pipistrellus nathusii" = "#8a2be2",
  "Pipistrellus pygmaeus" = "#b15928",
  "Pipistrellus spp." = "#fdbf6f",
  "Rhinolophus ferrumequinum" = "#e31a1c",
  "Rhinolophus hipposideros" = "#33a02c",
  "Nyctalus noctula" = "#cab2d6",
  "Nyctalus leisleri" = "#fb9a99",
  "Nyctalus spp." = "#eee8cd",
  "Eptesicus serotinus" = "#008b8b"
)

# count the number of pass per species
bat_pass_counts <- statics_plus %>%
  count(Species)

# get a vector of colours from the bat_pass_counts data.frame
colors <- unname(bat_colours_sci[bat_pass_counts$Species])

# plot a pie chart with plotly
plot_ly(bat_pass_counts,
  labels = ~Species, values = ~n,
  type = "pie",
  marker = list(colors = colors)
) %>%
  layout(
    title = "Bat Species Passes",
    xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
    yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
  )
Figure 5: Interactive Pie Chart of Species Passes

2.4 Bubble Chart

The coloured bubbles on Figure 6 show bats around a west Devon Oak tree, on the bottom scale is the hour after sunset the bat was observed The length of time the bat was present, in seconds, is expressed by the size of the bubble; a small bubble indicates the bat is likely to be travelling and a larger bubble could mean that the bat was feeding (foraging). Hovering the mouse over the coloured circle will give the bat name and number of seconds the bat was present. A similar graphic was produced in Figure 2 using ggplot which was converted to a plotly chart using ggplotly.

Code
library(tidyverse)
library(iBats)
library(plotly)


# bat species colours
bat_colours_sci <- c(
  "Barbastella barbastellus" = "#1f78b4",
  "Myotis alcathoe" = "#a52a2a",
  "Myotis bechsteinii" = "#7fff00",
  "Myotis brandtii" = "#b2df8a",
  "Myotis mystacinus" = "#6a3d9a",
  "Myotis nattereri" = "#ff7f00",
  "Myotis daubentonii" = "#a6cee3",
  "Myotis spp." = "#bcee68",
  "Plecotus auritus" = "#8b0000",
  "Plecotus spp." = "#8b0000",
  "Plecotus austriacus" = "#000000",
  "Pipistrellus pipistrellus" = "#ffff99",
  "Pipistrellus nathusii" = "#8a2be2",
  "Pipistrellus pygmaeus" = "#b15928",
  "Pipistrellus spp." = "#fdbf6f",
  "Rhinolophus ferrumequinum" = "#e31a1c",
  "Rhinolophus hipposideros" = "#33a02c",
  "Nyctalus noctula" = "#cab2d6",
  "Nyctalus leisleri" = "#fb9a99",
  "Nyctalus spp." = "#eee8cd",
  "Eptesicus serotinus" = "#008b8b"
)
xtitle <- "Hours After Sunset"
ytitle <- "Circles spread across the axis so they can all be seen"

f1 <- list(
  family = "Arial, sans-serif",
  size = 18,
  color = "lightgrey"
)

ay <- list(title = ytitle, 
           showline = FALSE, 
           showticklabels = FALSE, 
           showgrid = TRUE,
           titlefont = f1)

ax <- list(title = xtitle,
           titlefont = f1,
           tickfont = f1)

  
  
Bubble <- TavyOak %>% 
    mutate(BatPass = jitter(rep(1, nrow(.))),
           `Time post sunset (hours)` = round(post_set_hr, digits = 2),
           `Recorded Activity (secs)`= round(bat_time, digits = 2),
           `Species` = Species) %>% 
    plot_ly(x = ~`Time post sunset (hours)`, y=~BatPass, alpha = 0.9,
            hoverinfo = 'text',
        text = ~paste(" Bat Species:", Common , "<br>",
                      "Length of bat activity (secs):", bat_time)) %>% 
    add_markers(color = ~`Species`, 
                colors = bat_colours_sci, 
                size = ~`Recorded Activity (secs)`, 
                sizes = c(50, 5000)) %>%
    layout(yaxis = ay, 
           xaxis = ax, 
           margin = list(l = 75, b =50), 
           paper_bgcolor='#000000', 
           plot_bgcolor='#191970') %>%
    hide_legend()


suppressWarnings(plotly_build(Bubble))
Figure 6: Interactive Bubble Chart of Species Activity for the TavyOak Data

3 Tables

3.1 datatable

The package datatable is used to create the interactive table below with the Lydford data from the iBats package.

  • post_set_min- time in minutes the bat was observed after sun set
  • bat_time - time in seconds the bat was observed
Code
Lydford %>% 
  select(DateTime, Species, post_set_min, bat_time) %>% 
  mutate(post_set_min = round(post_set_min, 2)) %>% 
  # show the data with datatable function from the DT packag
  DT::datatable(options = list(pageLength = 10), 
                caption = 'Principal Lydford Transect Data')

3.2 reactable

The package reactable is used to create the interactive table below with the statics data from the iBats package.

  • post_set_min- time in minutes the bat was observed after sun set
Code
# https://glin.github.io/reactable/

library(tidyverse)
library(reactable)
library(iBats)
library(hms)

Attaching package: 'hms'
The following object is masked from 'package:lubridate':

    hms
Code
# Add data and time information to the statics data using the iBats::date_time_info
statics_plus <- iBats::date_time_info(statics)

# Add sun and night time metrics to the statics data using the iBats::sun_night_metrics() function.
statics_plus <- iBats::sun_night_metrics(statics_plus) %>%
  select(Night, Location = Description, Month = MonthFull, Species, post_set_min) %>%
  mutate(post_set_min = as.integer(post_set_min))


reactable(statics_plus,
  defaultPageSize = 12,
  filterable = TRUE,
  searchable = TRUE,
  bordered = TRUE,
  highlight = TRUE,
  defaultColDef = colDef(
    header = function(value) gsub(".", " ", value, fixed = TRUE),
    cell = function(value) format(value, nsmall = 1),
    align = "center",
    format = colFormat(digits = 1),
    minWidth = 60,
    headerStyle = list(color = "#000000", background = "#CCCCCC")
  ),
  columns = list(
    Location = colDef(sticky = "left", minWidth = 60, sortable = TRUE),
    Night = colDef(minWidth = 60, sortable = TRUE, format = colFormat(date = TRUE, locales = "en-GB")),
    Species = colDef(minWidth = 100, sortable = TRUE),
    Month = colDef(minWidth = 60, sortable = TRUE),
    post_set_min = colDef(sortable = TRUE)
  )
)

Footnotes

  1. See the htmlwidgets for R page https://www.htmlwidgets.org/showcase_plotly.html plus the plotly r reference https://plotly.com/ggplot2/.↩︎

  2. For more information see https://plotly.com/r/bar-charts/↩︎

  3. For more information see https://plotly.com/r/bar-charts/↩︎

  4. For more information see https://plotly.com/r/pie-charts/↩︎