Contagious disease data for US states

library("dslabs")
## Warning: package 'dslabs' was built under R version 3.6.1
data(package="dslabs")
list.files(system.file("script", package = "dslabs"))
##  [1] "make-admissions.R"                   
##  [2] "make-brca.R"                         
##  [3] "make-brexit_polls.R"                 
##  [4] "make-death_prob.R"                   
##  [5] "make-divorce_margarine.R"            
##  [6] "make-gapminder-rdas.R"               
##  [7] "make-greenhouse_gases.R"             
##  [8] "make-historic_co2.R"                 
##  [9] "make-mnist_27.R"                     
## [10] "make-movielens.R"                    
## [11] "make-murders-rda.R"                  
## [12] "make-na_example-rda.R"               
## [13] "make-nyc_regents_scores.R"           
## [14] "make-olive.R"                        
## [15] "make-outlier_example.R"              
## [16] "make-polls_2008.R"                   
## [17] "make-polls_us_election_2016.R"       
## [18] "make-reported_heights-rda.R"         
## [19] "make-research_funding_rates.R"       
## [20] "make-stars.R"                        
## [21] "make-temp_carbon.R"                  
## [22] "make-tissue-gene-expression.R"       
## [23] "make-trump_tweets.R"                 
## [24] "make-weekly_us_contagious_diseases.R"
## [25] "save-gapminder-example-csv.R"
library(RColorBrewer)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.1
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(gapminder)
## Warning: package 'gapminder' was built under R version 3.6.1
## 
## Attaching package: 'gapminder'
## The following object is masked from 'package:dslabs':
## 
##     gapminder
library(gganimate)
## Warning: package 'gganimate' was built under R version 3.6.1

Original graph - Heatmap

data("us_contagious_diseases")
the_disease <- "Measles"
us_contagious_diseases <- us_contagious_diseases %>% 
  filter(!state %in% c("Hawaii","Alaska") & disease ==  the_disease) %>%
  mutate(rate = count / population * 10000 * 52 / weeks_reporting) %>%
  mutate(state = reorder(state, rate))

us_contagious_diseases1 <- ggplot(us_contagious_diseases, aes(x = year, y = 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=1963, col = "blue") +
      theme_minimal() +  theme(panel.grid = element_blank()) +
      ggtitle(the_disease, "US States") +
      ylab("State") +
      xlab("Year")
us_contagious_diseases1

Scatter plot animated

There are three variables for this gragh: States, years, and rate. The third variable is same as in the original graph - Heatmap - and uses multiple colors to show it. The data set used in this code is the filtered and mutated data set from above. You can hit the play button to begin the animation, and it will show all of the information regarding rate for each state in a frame. I couldn’t change values for the X-axis to show the appropriate numbers, so instead, they show the scientific notation.

us_contagious_diseases2 <- ggplot(us_contagious_diseases, 
                                  aes(x = rate, y = state, frame = year, color = rate)) +
                        geom_point(aes(colour = rate, 
                         text = 
                           paste(paste("Rate: ", us_contagious_diseases$rate),
                           paste("Year: ", us_contagious_diseases$year),
                           paste("State: ", us_contagious_diseases$state),
                           paste("Count: ", us_contagious_diseases$count),
                           paste("population: ", us_contagious_diseases$population),
                           paste("Weeks reported: ", us_contagious_diseases$weeks_reporting),
                           sep = "<br />")
                           , alpha = 10)) +
                        ggtitle("Measles: US States") +
                        xlab("Rate") +
                        ylab("State") +
                        theme_minimal(base_size = 10) +
                        theme(legend.position="Rate") +
                        scale_x_log10() +
                        scale_colour_gradientn(colours=rainbow(4))
## Warning: Ignoring unknown aesthetics: text
us_contagious_diseases2 <- ggplotly(us_contagious_diseases2,
                                    height = 900, width = 700,
                                    tooltip = "text") %>%
                        animation_opts(frame = 200,
                                    redraw = FALSE)
## Warning: Transformation introduced infinite values in continuous x-axis
us_contagious_diseases2