R/map_stats.R

Defines functions map_stats

Documented in map_stats

#-------------------------------------------------------------------
#                Create a cloropleth map for given stats
#-------------------------------------------------------------------

#Contains 2 functions to visualize states related data


#' Create a cloropleth map.
#'
#' Plots a cloropleth map for the given stats.
#'
#' This function plots summarised data by states in the us map.
#'
#' \emph{*Note: It doesn't include states outside the us main
#'   land (Hawaii, PR and alaska)}
#'
#' @param data \bold{Data frame}, must contain 2 columns, \code{state_id} &
#'   the \emph{stat to use}. Currently there are two data sets can be
#'   used here, \emph{women_population_data.csv & median_income_by_state.csv}
#' @param column \bold{Character}, specifies the \emph{ stat to use},
#'   should match a column present in \code{data}
#' @param colour \bold{Numeric}, ranges from 1 to 7, defaults to 2,
#'   the classic Nadine west pink.
#' @param label \bold{Character}, \emph{'dollar'} if the stat is a currency,
#'   \emph{'percent'} if it is proportion,
#'   \emph{'none'} otherwise.
#' @examples
#' income <- read.csv('/Data/Auxiliary_data/median_income_by_state.csv')
#' map_stats(income, "median_income", 4, "dollar")
#' map_stats(income, "median_income")
#'
#' @return A cloropleth map of the U.S with the given stat.
#'
#' @import ggplot2
#' @export
map_stats <- function(data, column, colour = 2, label = 'none'){

  if ("state" %in% colnames(data))
    data %<>% select(-state)

  #Setup colours and theme for ggplot
  colours = c('#3FB8AF', '#FF3D7F', '#4C49A2', '#029DAF', '#FFC219', '#F07C19',
              '#4C49A2')
  theme_set(theme_minimal())

  #Clean the states file
  #data(states)
  states <- states %>%
    select(V1,V2) %>% mutate(V2 = tolower(V2))

  colnames(states) <- c('state_id', 'state')

  #Join with the  data
  data <- left_join(data, states, by = 'state_id') %>%
    filter(!state%in%c('puerto rico','hawaii', 'alaska'))

  #Create map data
  states_map <- map_data("state")

  plot_map <- ggplot(data, aes(map_id = state)) +
    geom_map(aes_string(fill = column), map = states_map, colour = '#f2f2f2') +
    expand_limits(x = states_map$long, y = states_map$lat)+
    labs(x=NULL, y=NULL) +
    coord_map("albers", lat0 = 39, lat1 = 45) +
    theme(panel.border = element_blank()) +
    theme(panel.background = element_blank()) +
    theme(axis.ticks = element_blank())+
    theme(axis.text = element_blank())+
    scale_y_continuous(breaks=c()) +
    scale_x_continuous(breaks=c()) +
    theme(panel.border = element_blank())

  if(label=='percent'){
    plot_map <- plot_map +
      scale_fill_continuous(high=colours[colour], low='white', guide='colorbar',
                            label = scales::percent)
  }else if(label=='dollar'){
    plot_map <- plot_map +
      scale_fill_continuous(high=colours[colour], low='white', guide='colorbar',
                            label = scales::dollar)
  }else{
    plot_map <- plot_map +
      scale_fill_continuous(high=colours[colour], low='white', guide='colorbar',
                            label = scales::comma)
  }


  if('Min_Date' %in% colnames(data) & 'Max_Date' %in% colnames(data)){
    plot_map <- plot_map + labs(caption = paste('From', format(min(data$Min_Date), "%b, %Y"),
                                   "to", format(max(data$Max_Date), "%b, %Y")))
  }
    plot_map
}
shahreyar-abeer/nadinewestr documentation built on May 27, 2019, 1:06 a.m.