These materials were developed by Sam Csik & Juliette Verstaen using UFO Sighting Data from the National UFO Reporting Center (NUFORC). Data can be downloaded from kaggle. Source code can be found on GitHub.
In this example report, we demonstrate how to use a variety of different HTML widgets
that allow for interactive web visualizations, as well as how to leverage R Markdown and GitHub Pages for building web pages to display reports (or whatever else you might dream of creating with RMarkdown).
Expand hidden Code
chunks to see how we’ve created the following tables, maps, and plots. Also, be on the lookout for TIPs and RESOURCEs throughout for pointers and links to resources!
And finally, before we get started, the R Markdown Cookbook by Yihui Xie, Christophe Dervieux, & Emily Riederer and R Markdown: The Definitive Guide by Yihui Xie, J. J. Allaire, & Garrett Grolemund contain tons of incredible information and answers to nearly all of the R Markdown-related questions we have encountered. We recommend coming back to these to learn more about all-things R Markdown.
###########################
# load packages
###########################
# for cleaning and wrangling
library(tidyverse)
library(janitor)
library(lubridate)
# for map making and general spatial work
library(leaflet)
library(mapview)
library(sf)
# for working with time series data
library(dygraphs)
library(xts)
# for making tables
library(DT)
library(gt)
# for palette making
library(RColorBrewer)
library(colorspace)
library(scales)
# for reproducibility
library(here)
##############################
# source custom alien color palette
##############################
source(here::here("code", "palettes.R"))
###########################
# load data
###########################
UFO_data_all <- read_csv(here::here("data", "UFO_complete.csv"))
###########################
# bunch of parsing failures from columns that are shifted; we're going to just filter those out bc they don't have appropriate lat/long data
###########################
# see parsing failures here
UFO_parsing_failures <- UFO_data_all %>%
filter(is.na(city))
# remove those from the dataset
UFO_data_cleaned <- UFO_data_all %>%
filter(!is.na(city))
dygraph
)##############################
# get colors from our custom alien palette
##############################
colors <- alien_palette("galaxy_quest", 8, "discrete")
##############################
# wrangle data
##############################
UFO_dygraph <- UFO_data_cleaned %>%
clean_names() %>%
select(-date_posted) %>%
separate(datetime, c("month", "second", "third"), sep = "/") %>%
separate(third, c("year", "time"), sep = " ") %>%
group_by(year) %>%
count() %>%
rename(number_instances = n) %>%
filter(!is.na(year)) %>%
mutate(date_seen = paste("01/01/", year, sep = "")) %>%
mutate(date_seen = as.character(date_seen),
## needs to be in date time format/class in order to be converted into extendable time series below
date_seen = as.Date(date_seen, format = "%m/%d/%Y"))
##############################
# create time series object
##############################
ufo_timeseries <- xts(x = UFO_dygraph$number_instances,
order.by = UFO_dygraph$date_seen)
##############################
# finally create the plot
##############################
dy_plot <- dygraph(ufo_timeseries, main = "Aliens from Earth") %>%
dyRangeSelector() %>%
dyOptions(labelsUTC = TRUE, fillGraph=TRUE, fillAlpha=0.1, drawGrid = FALSE, colors= colors[1]) %>%
dyCrosshair(direction = "vertical") %>%
dyHighlight(highlightCircleSize = 5, highlightSeriesBackgroundAlpha = 0.2, hideOnMouseOut = FALSE) %>%
dyRoller(rollPeriod = 1) %>%
dyAxis("y", label = "Number of sightings, abductions, etc")
##############################
# display plot in knitted doc
##############################
dy_plot
RESOURCE: For more information and tips on (1) creating dygraphs, check out dygraphs for R (2) xts objects check this out
mapview
)##############################
# clean data
##############################
UFO_time <- UFO_data_cleaned %>%
clean_names() %>%
select(-date_posted) %>%
mutate(longitude = as.numeric(longitude),
latitude = as.numeric(latitude)) %>%
mutate(duration_length = case_when(duration_seconds <= 60 ~ "short",
duration_seconds >= 360 ~ "long",
T ~ "medium")) %>%
filter(!is.na(longitude),
!is.na(latitude)) %>%
group_by(duration_length, longitude, latitude) %>%
count() %>%
rename(number_instances = n)
##############################
# need to convert to a spatial feature object
##############################
UFO_spatial <- st_as_sf(UFO_time, coords = c("longitude", "latitude"),
crs = 4326)
##############################
# create the map!
##############################
UFO_map_mv <- mapview(UFO_spatial, zcol = "duration_length", cex = "number_instances", alpha = 0.2)
##############################
# display map in knitted doc
##############################
UFO_map_mv