Skip to content

Instantly share code, notes, and snippets.

@ajstewartlang
Last active December 5, 2019 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ajstewartlang/d40ae89ba2970a960975af7868933c16 to your computer and use it in GitHub Desktop.
Save ajstewartlang/d40ae89ba2970a960975af7868933c16 to your computer and use it in GitHub Desktop.
# Note, not all of these libraries are needed for the code below
library(tidyverse)
library(openintro)
library(ggthemes)
library(ggmap)
library(maps)
library(mapdata)
library(ggrepel)
library(patchwork)
library(lubridate)
# read in data
ufo_sightings <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-06-25/ufo_sightings.csv")
# bar plot of top 10 US states with number of sightings in each state
# the abbr2state() function converts state abbreviations to full names
plot1 <- ufo_sightings %>%
mutate(state = abbr2state(state)) %>%
filter(!is.na(state)) %>%
group_by(state) %>%
tally() %>%
top_n(10) %>%
ggplot(aes(x = reorder(state, n), y = n, fill = state)) +
geom_col() +
coord_flip() +
guides(fill = FALSE) +
labs(title = "Top 10 States for UFO Sightings",
x = NULL,
y = NULL) +
ylim(0, 11000) +
theme_minimal() +
theme(text = element_text(size = 15))
# work out the top 10 states with UFO sightings
top_states <- ufo_sightings %>%
mutate(state = abbr2state(state)) %>%
filter(!is.na(state)) %>%
group_by(state) %>%
tally() %>%
top_n(10) %>%
pull(state)
# work out states within lat and long limits (i.e., exclude Alaska)
tidied_ufo <- ufo_sightings %>%
filter(country == "us") %>%
filter(latitude > 24 & latitude < 50) %>%
mutate(state = abbr2state(state))
# plot all sightings on a map of the US, with 10 top states coloured
plot2 <- tidied_ufo %>%
ggplot(aes(x = longitude, y = latitude)) +
geom_point(size = .5, alpha = .25) +
geom_point(data = filter(tidied_ufo, state %in% top_states),
aes(colour = state), size = .5, alpha = .25) +
geom_polygon(data = states,
aes(x = long, y = lat, fill = region, group = group), alpha = .05) +
theme_void() +
coord_cartesian() +
guides(colour = FALSE) +
guides(fill = FALSE)
# bar plot of top 10 UFO shapes spotted in California
plot3 <- tidied_ufo %>%
filter(state == "California") %>%
filter(ufo_shape != "other") %>%
filter(ufo_shape != "unknown") %>%
group_by(ufo_shape) %>%
tally() %>%
top_n(10) %>%
mutate(ufo_shape = str_to_title(ufo_shape)) %>%
ggplot(aes(x = reorder(ufo_shape, n), y = n, fill = ufo_shape)) +
geom_col() +
coord_flip() +
guides(fill = FALSE) +
labs(title = "Top 10 UFO Shapes spotted in California",
x = NULL,
y = NULL) +
theme_minimal() +
theme(text = element_text(size = 15))
# Plot 4
# read in dataset which contains lat and long of US states and cities
uscities <- read_csv("simplemaps_uscities_basicv1/uscities.csv")
# extract data just about states
states <- map_data("state")
# create a data frame of the top 10 most populous states in California
big_cal <- uscities %>%
filter(state_name == "California") %>%
arrange(-population) %>%
top_n(10, population)
# plot UFO sightings in California and add points representing the 10 largest cities
plot4 <- tidied_ufo %>%
filter(state == "California") %>%
ggplot(aes(x = longitude, y = latitude)) +
geom_polygon(data = filter(states, region == "california"),
aes(x = long, y = lat, fill = region, group = group), alpha = .2, color = "#D89000") +
geom_point(size = 2, alpha = .5, colour = "#D89000") +
geom_text_repel(data = big_cal, aes(x = lng, y = lat, label = city), size = 5) +
geom_point(data = big_cal, aes(x = lng, y = lat), size = 2, alpha = .5) +
guides(fill = FALSE) +
theme_void() +
coord_cartesian() +
labs(caption = "A subset of the Tidy Tuesday UFO Sightings around the world dataset")
# Put plots together
(plot1 + plot3) / (plot2 + plot4)
# Let's animate - plot sightings in the world animated by year
ufo_sightings %>%
filter(!is.na(latitude)) %>%
filter(!is.na(longitude)) %>%
separate(date_time, into = c("month", "day", "year"), sep = "/") %>%
separate(year, into = c("year", "time"), sep = " ") %>%
mutate(year = as.integer(year)) %>%
ggplot(aes(x = longitude, y = latitude)) +
geom_point() +
transition_manual(year, cumulative = TRUE) +
theme_void()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment