## -----------------------------------------------------------------------------------------------------
library(dslabs)
library(dplyr)
library(ggplot2)
dat <- us_contagious_diseases %>%
  filter(year == 1967 & disease == "Measles" & !is.na(population)) %>%
  mutate(rate = count / population * 10000 * 52 / weeks_reporting)

  dat |>
  mutate(state = forcats::fct_reorder(state, rate, .desc = F)) |>
  ggplot(aes(state, rate)) +
  geom_col() +
  coord_flip()


## -----------------------------------------------------------------------------------------------------

library(RColorBrewer)
the_disease <- "Smallpox"
dat <- us_contagious_diseases |>
  filter(!state %in% c("Hawaii","Alaska") & disease == the_disease) |>
  mutate(rate = count / population * 10000 * 52 / weeks_reporting) |> 
  mutate(state = reorder(state, ifelse(year <= 1963, rate, NA), 
                         median, na.rm = TRUE)) 

dat |> ggplot(aes(year, state, fill = rate)) +
  geom_tile(color = "grey50") +
  scale_x_continuous(expand = c(0,0)) +
  scale_fill_gradientn(colors = brewer.pal(9, "Reds"), trans = "sqrt") +
  geom_vline(xintercept = 1940, col = "blue") +
  theme_minimal() +  
  theme(panel.grid = element_blank(), 
        legend.position = "bottom", 
        text = element_text(size = 8)) +
  labs(title = the_disease, x = "", y = "")




## -----------------------------------------------------------------------------------------------------
library(tidyverse)
library(dslabs)
library(RColorBrewer)

data("us_contagious_diseases")


#the_disease <- "Measles"
dat <- us_contagious_diseases |>
  filter(state %in% c("California")  & weeks_reporting >= 10 ) |>
  mutate(rate = count / population * 10000 * 52 / weeks_reporting) |> 
  mutate(state = reorder(state, ifelse(year <= 1963, rate, NA), 
                         median, na.rm = TRUE)) 
                         
                         
avg <- us_contagious_diseases  |> group_by(year) |>
  summarize(us_rate = sum(count, na.rm = TRUE) / 
              sum(population, na.rm = TRUE) * 10000)



dat |> 
  filter(!is.na(rate)) |>
    ggplot() +
  geom_line(aes(year, rate, group = disease,  color = disease), 
            show.legend = T, alpha = 0.2, linewidth = 1) +
  geom_line(mapping = aes(year, us_rate),  data = avg, linewidth = 1) +
  scale_y_continuous(trans = "sqrt", breaks = c(5, 25, 125, 300)) + 
  ggtitle("Cases per 10,000") + 
  xlab("") + ylab("") +
  geom_text(data = data.frame(x = 1955, y = 50), 
            mapping = aes(x, y, label = "Average Disease Rate"), 
            color = "black") + 
  geom_vline(xintercept = 1963, col = "blue") 
  


## -----------------------------------------------------------------------------------------------------
library(dplyr)
library(ggplot2)
library(dslabs)

p <- ggplot(data = murders)
class(p)


## -----------------------------------------------------------------------------------------------------
murders |> ggplot(aes(x = population, y = total)) +
  geom_point()



## -----------------------------------------------------------------------------------------------------
murders |> ggplot(aes(population, total, label = abb)) +
  geom_label()


## -----------------------------------------------------------------------------------------------------
p <- murders |> ggplot(aes(population, total, label = abb, color = region)) +
  geom_label()
p + scale_x_log10()


## -----------------------------------------------------------------------------------------------------
heights |> ggplot(aes(height, fill = sex)) +
  geom_density(alpha = 0.2)

