Getting Data

library(readxl)
## Warning: package 'readxl' was built under R version 4.0.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.4     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 4.0.3
## Warning: package 'tibble' was built under R version 4.0.3
## Warning: package 'stringr' was built under R version 4.0.3
## Warning: package 'forcats' was built under R version 4.0.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggmap)
## Warning: package 'ggmap' was built under R version 4.0.3
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
mapdt <- read_excel("C:/Users/Anhuynh/Documents/My Tableau Repository/Datasources/2020.4/en_US-US/Sample - Superstore.xls")
library(readr)

mapSource <- read_csv("C:/Users/Anhuynh/Desktop/Data Science_Cousera/Developing Data Products/Assignment/data_locations.csv")
## Parsed with column specification:
## cols(
##   City = col_character(),
##   lon = col_double(),
##   lat = col_double()
## )
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.0.3
set.seed(2021-01-26)

mapdt_sub2 <- subset(mapdt, City %in% mapSource$City, select = c(City, Profit, `Ship Mode`) )
map_join <- full_join(mapdt_sub2, mapSource, by = c("City" = "City"))


map_join %>% leaflet() %>%
        addTiles() %>%
        addMarkers(clusterOptions = markerClusterOptions(freezeAtZoom = 5)) %>%
        addCircles(weight = 1, radius = sqrt(map_join$Profit) * 30)
## Assuming "lon" and "lat" are longitude and latitude, respectively
## Assuming "lon" and "lat" are longitude and latitude, respectively
## Warning in sqrt(map_join$Profit): NaNs produced

Figure 01: Profits Change Across Cities.

library(leaflet)

mapdt_sub3 <- subset(mapdt, City %in% mapSource$City, select = c(City, `Ship Mode`))
map_join_mod <- full_join(mapdt_sub3, mapSource, by = c("City" = "City"))

# Create a palette that maps factor levels to colors
pal <- colorFactor(c("green", "orange", "navy", "red"), domain = c("First Class", "Same Day", "Second Class", "Standard Class"))

leaflet(map_join_mod) %>% addTiles() %>%
  addCircleMarkers(
    radius = ifelse(map_join_mod$`Ship Mode` == "First Class", 4,
                     ifelse(map_join_mod$`Ship Mode` == "Same Day", 6,
                             ifelse(map_join_mod$`Ship Mode` == "Second Class", 8, 10)
                     ) ),
    color = pal(map_join_mod$`Ship Mode`),
    stroke = FALSE, fillOpacity = 0.5
  ) %>%
  addLegend(labels = c("First Class", "Same Day", "Second Class", "Standard Class"), colors = c("green", "orange", "navy", "red"))
## Assuming "lon" and "lat" are longitude and latitude, respectively

Figure 02: Ship Mode Across Cities