---
title: 'Stat 480 - Homework #3'
author: "Your Name"
date: "2/6/2020"
output: 
  html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
	echo = TRUE,
	message = FALSE,
	warning = FALSE
)
```


<br/>

#### Submission Details

**Due date**: the homework is due before class on Thursday. 

**Submission process**: submit both the R Markdown file and the corresponding html file on canvas. Please submit both the `.Rmd` and the `.html` files separately and do not zip the two files together.

<br/>

#### Spotify data

1. Download the RMarkdown file with these homework instructions to use as a template for your work. Make sure to replace "Your Name" in the YAML with your name.  


2. Using the command below, read in the spotify data set into your R session. 

```{r}
spotify <- read.csv("https://raw.githubusercontent.com/Stat480-at-ISU/Stat480-at-ISU.github.io/master/homework/data/spotify.csv")
```


3. Use one of our object inspecting functions and interpret the result in the data that you see.

```{r}
str(spotify)
```

This data set contains 13 variables and 10,000 observations. Of the 13 variables, 3 are of type `factor`, 1 of type `integer`, and the rest of `numeric`.



4. Use the package `ggplot2` to draw a barchart of the genres. In addition, map the genre categories to the fill color of the barchart.

```{r}
library(ggplot2)
ggplot(spotify, aes(x = playlist_genre, fill = playlist_genre)) + geom_bar()
```

This is a bar chart with the categorical variable `playlist_genre` mapped to the x axis and to the fill color. The heights of the bars represent the number of songs that fall within that bar's category. From this plot we gain the impression that there is *roughly* an equal number of songs for each category in this dataset.


5. Use the package `ggplot2` to draw a histogram of one of the continuous variables in the dataset. Use fill color to show the genre categories and adjust the binwidth if necessary. Use `facet_wrap()` to create a histogram for each of the genre categories.

```{r}
ggplot(spotify, aes(x = duration_min, fill = playlist_genre)) + geom_histogram()
ggplot(spotify, aes(x = duration_min, fill = playlist_genre)) + geom_histogram() + facet_wrap(~playlist_genre)
```

This is a histogram with the quantitative variable `duration_min` mapped to the x axis and the categorical variable `playlist_genre` mapped to the fill color. The heights of the bars represent the number of songs that have a duration within that bin. From this plot we gain the impression that most songs are 3-4 minutes long and there are only a few songs less than a minute and only a few songs more than 7 minutes. There is not a large difference in the distributions of the different genres.


6. Use the package `ggplot2` to draw a scatterplot to compare the length of the song with the energy measure. Use color to show the genre categories.

```{r}
ggplot(spotify, aes(y = energy, x = duration_min, color = playlist_genre)) + geom_point()
```

This is a scatterplot with the quantitative variables `duration_min` and `energy` mapped to the x and y axis, respectively. In addition, the categorical variable `playlist_genre` is mapped to the color of the points. From this plot we again gain the impression that most songs are 3-4 minutes long and there are only a few songs less than a minute and only a few songs more than 7 minutes. There is does not appear to be a relationship between `duration_min` and `energy`, nor is there a large difference in the different genres.


7. For each of the three figures above, write a two-three sentence summary, describing the
    a. **structure of the plot**: what type of plot is it? Which variables are mapped to x, to y, and to the (fill) color?
    b. **main message of the plot**: what is your main finding, i.e. what do you want viewers to learn from the plot?
    c. **additional message**: point out anomalies or outliers, if there are any. 

