# Density and histogram examples 

I've put together a few (rough, unformatted) examples to give you an idea of how we can examine variable distributions. Enjoy!

Load atlas and related packages, create a data frame, `michigan` that is restricted to, well, Michigan.

```{r}
library(tidyverse)
library(haven)


atlas <- read_dta("atlas.dta")


michigan <- filter(atlas,state == 26)

```

Let's make a [histogram](https://ggplot2.tidyverse.org/reference/geom_histogram.html?q=geom%20_%20his) with two variables. Notice that when everything is in the same frame, you can just layer on the plots! But, you would want to call the aesthetics within the each layer. 

```{r}
ggplot(michigan) + 
    geom_histogram(aes(kfr_black_p25,after_stat(count),na.rm=TRUE)) + 
    geom_histogram(aes(kfr_black_p75,after_stat(count),na.rm=TRUE)) + xlim(0,100000)
```

Yikes! That doesn't looks good. Let's try harder: 

```{r}
ggplot(michigan) + 
    geom_histogram(aes(kfr_black_p25,after_stat(count),fill = "red", alpha = 0.2),binwidth=5000,na.rm=TRUE) + 
    geom_histogram(aes(kfr_black_p75,after_stat(count),fill = "blue", alpha = 0.2,na.rm=TRUE),binwidth=5000,na.rm=TRUE) + xlim(0,100000)
```

Is there a better way? 

Try [`geom_density()`](https://ggplot2.tidyverse.org/reference/geom_density.html)

```{r}
ggplot(michigan) + 
    geom_density(aes(kfr_black_p25,after_stat(count)),na.rm=TRUE) + 
    geom_density(aes(kfr_black_p75,after_stat(count)),na.rm=TRUE) + xlim(0,100000)
```

Follow the help and you can do all sorts of things! 



```{r}
ggplot(michigan) + 
    geom_density(aes(kfr_black_p25,after_stat(count),fill = "blue",na.rm=TRUE),alpha = 0.1) + 
    geom_density(aes(kfr_black_p75,after_stat(count),fill = "red",na.rm=TRUE),alpha = 0.1) + xlim(0,100000)
```


Or, you can do something fancy like a violin plot! Here, you need some sort of "x" variable. If you have a small enough geographic area, you could do something county (good for VT, bad for MI)

```{r}

# Generate categorical variable for your y axis

# Take a look to get a sense
michigan %>% summarize(mean(frac_coll_plus2010, na.rm = TRUE), quantile(frac_coll_plus2010, na.rm = TRUE,0.25), quantile(frac_coll_plus2010, na.rm = TRUE,0.75))


# Cut into 4
xs<- quantile(michigan$frac_coll_plus2010,c(0,1/3,2/3,1), na.rm = TRUE)

# Generate cqtegorical variable 

michigan <- michigan %>% mutate(category=cut(frac_coll_plus2010, breaks=xs, labels=c("1st tercile","2nd tercile","3rd tercile"),na.rm=TRUE))

# Here I trimmed the y for interpretation, but it's not always best practice
ggplot(michigan,aes(factor(category),kfr_black_p25)) + 
    geom_violin(na.rm=TRUE) + 
    geom_jitter(height = 0.3,width = 0.3,size =.1,na.rm=TRUE) + 
  scale_x_discrete(na.translate = FALSE)  + 
  ylim(0,100000)
  
```

