---
title: "Homework 4"
output:
  html_document:
    df_print: paged
    theme:
      version: 4
---

```{r global_options, include=FALSE}
library(knitr)
library(tidyverse)
library(colorspace)
opts_chunk$set(fig.align="center", fig.height=4, fig.width=5.5)

# data prep:
ufo_sightings <- 
  read_csv("https://wilkelab.org/classes/SDS348/data_sets/ufo_sightings_clean.csv") %>%
  separate(datetime, into = c("month", "day", "year"), sep = "/") %>%
  separate(year, into = c("year", "time"), sep = " ") %>%
  separate(date_posted, into = c("month_posted", "day_posted", "year_posted"), sep = "/") %>%
  select(-time, -month_posted, -day_posted) %>%
  mutate(
    year = as.numeric(year),
    state = toupper(state)
  ) %>%
  filter(!is.na(country))
```

**This homework is due on Feb. 29, 2024 at 11:00pm. Please submit as a pdf file on Canvas.**

In this homework, we will work with the `ufo_sightings` dataset:

```{r}
head(ufo_sightings)
```

The main columns we will use are `year` (the year of the sighting), `city` (the city in which the sighting was reported), and `state` (the state in which the sighting was reported).

**Problem 1:  (8 pts)** Since 1980 (inclusive), what are the top 15 cities that have reported the most UFO sightings? Create a new dataframe to answer the question. No plots are necessary.

(Hint: You can use `slice(1:15)` to select the first fifteen rows in a data frame.)

```{r}
# your code here
```


**Problem 2: (8 pts)** 

Next, how has the number of UFO sightings changed for five states since 1970? Please follow these steps:

1. Filter the dataset to keep the following five states: CA, NY, TX, AZ, ID
2. Keep only the records from 1970 and onwards. 
3. Find the number of records for each year and state. 
4. Output the new table below your code block.

Your final table should be in long format and have three columns, `year`, `state`, and `count`. You will plot this table in Problem 3.

```{r}
# your code here
```

**Problem 3: (4 pts)** 

Use the new dataframe you made in Problem 2 and add an appropriate color scale from the `colorspace` package to the plot below.

```{r eval = FALSE}
your_dataframe_here %>% # use the dataframe from Problem 2 here, and set eval = TRUE in the chunk header 
 ggplot(aes(x = year, y = count, color = state)) +
 geom_line() +
 xlab("Year") +
 ylab("UFO Sightings (Count)") +
 theme_bw()
```




