animationproject

### Animated map of UFO sightings 

### 1. - libraries and data
library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.3.6     ✔ purrr   0.3.4
✔ tibble  3.1.7     ✔ dplyr   1.0.9
✔ tidyr   1.2.0     ✔ stringr 1.4.0
✔ readr   2.1.2     ✔ forcats 0.5.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(rnaturalearthdata)
library(rnaturalearth)
library(ggthemes)
library(sf)
Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(transformr)

Attaching package: 'transformr'

The following object is masked from 'package:sf':

    st_normalize
library(gifski)
library(tigris)
To enable caching of data, set `options(tigris_use_cache = TRUE)`
in your R script or .Rprofile.
library(gganimate)
library(readxl)


d <- read_excel("/Users/haydenbharper/Downloads/I'm Doing Data Science??/political-analysis-in-R-main/R/week-10-time/UFOs_coord.xlsx")

# filter data for just US sightings and only for defined UFO shapes

d <- d |>
  filter(Country != 'CANADA') |>
  filter(Shape != 'Unknown') |>
  filter(Shape != 'Other') |>
  group_by(State, City)

# this data is all from 2016 so animating the shape of the percieved UFO makes more sense than animating by time

### 2. - make the maps

#points showing UFO siting locations
p1 <- ggplot(data = d, mapping = aes(x = lng, y = lat)) +
  geom_point() +
  theme_map()

m <- ne_countries(country = 'United States of America', returnclass = 'sf')

#map of the USA
p2 <- ggplot() +
  geom_sf(data = m) +
  theme_map()

p2

# chage geo settings to be map friendly
st_crs(m)
Coordinate Reference System:
  User input: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
  wkt:
BOUNDCRS[
    SOURCECRS[
        GEOGCRS["unknown",
            DATUM["World Geodetic System 1984",
                ELLIPSOID["WGS 84",6378137,298.257223563,
                    LENGTHUNIT["metre",1]],
                ID["EPSG",6326]],
            PRIMEM["Greenwich",0,
                ANGLEUNIT["degree",0.0174532925199433],
                ID["EPSG",8901]],
            CS[ellipsoidal,2],
                AXIS["longitude",east,
                    ORDER[1],
                    ANGLEUNIT["degree",0.0174532925199433,
                        ID["EPSG",9122]]],
                AXIS["latitude",north,
                    ORDER[2],
                    ANGLEUNIT["degree",0.0174532925199433,
                        ID["EPSG",9122]]]]],
    TARGETCRS[
        GEOGCRS["WGS 84",
            DATUM["World Geodetic System 1984",
                ELLIPSOID["WGS 84",6378137,298.257223563,
                    LENGTHUNIT["metre",1]]],
            PRIMEM["Greenwich",0,
                ANGLEUNIT["degree",0.0174532925199433]],
            CS[ellipsoidal,2],
                AXIS["latitude",north,
                    ORDER[1],
                    ANGLEUNIT["degree",0.0174532925199433]],
                AXIS["longitude",east,
                    ORDER[2],
                    ANGLEUNIT["degree",0.0174532925199433]],
            ID["EPSG",4326]]],
    ABRIDGEDTRANSFORMATION["Transformation from unknown to WGS84",
        METHOD["Geocentric translations (geog2D domain)",
            ID["EPSG",9603]],
        PARAMETER["X-axis translation",0,
            ID["EPSG",8605]],
        PARAMETER["Y-axis translation",0,
            ID["EPSG",8606]],
        PARAMETER["Z-axis translation",0,
            ID["EPSG",8607]]]]
d <- st_as_sf(d, 
                coords = c('lng', 'lat'),
                crs = st_crs(m))

### 3. - combine and refine

p3 <- ggplot() + 
  geom_sf(data = m) +
  geom_sf(data = d, mapping = aes(color = Shape)) +
  #labs() +
  theme_map()

p3

p3 + transition_states(Shape, 
                       state_length = 25)

p4 <- ggplot() +
  geom_sf(data = m) +
  geom_sf(data = d, mapping = aes(color = Shape)) +
  labs(title = '2016 UFO Sightings in the USA',
       caption = 'Data source: Alejandro Manuel Arranz López from Data.World') +
  theme_map()

p4 + enter_fade() + transition_states(Shape, state_length = 10) + exit_fade()