require(readr); require(dplyr)
require(ggplot2); require(ggbur)
require(sf)
concerts <- read_csv("TheInternetConcerts.csv")
table(concerts$State)
## 
##              Arizona           California District of Columbia 
##                    1                   24                    1 
##              Georgia             Illinois            Louisiana 
##                    5                    4                    2 
##             Maryland        Massachusetts             Michigan 
##                    6                    4                    1 
##            Minnesota               Nevada             New York 
##                    1                    1                    7 
##       North Carolina               Oregon         Pennsylvania 
##                    3                    1                    3 
##            Tennessee                Texas             Virginia 
##                    2                    6                    1 
##           Washington 
##                    4
count2016 <- concerts %>% filter(Year == 2016)%>% count(State, name = "concerts") %>% arrange(desc(2016))
count2017 <- concerts %>% filter(Year == 2017)%>% count(State, name = "concerts") %>% arrange(desc(2017))
count2018 <- concerts %>% filter(Year == 2018)%>% count(State, name = "concerts") %>% arrange(desc(2018))
countTotal <- concerts %>% count(State, name = "concerts") %>% arrange(desc(concerts))

head(count2016)
## # A tibble: 6 × 2
##   State                concerts
##   <chr>                   <int>
## 1 Arizona                     1
## 2 California                  3
## 3 District of Columbia        1
## 4 Georgia                     1
## 5 Illinois                    1
## 6 Louisiana                   1
head(countTotal)
## # A tibble: 6 × 2
##   State      concerts
##   <chr>         <int>
## 1 California       24
## 2 New York          7
## 3 Maryland          6
## 4 Texas             6
## 5 Georgia           5
## 6 Illinois          4
US <- st_read("cb_2018_us_state_20m.shp")
## Reading layer `cb_2018_us_state_20m' from data source 
##   `C:\Users\iiStr\Documents\R course\Project\cb_2018_us_state_20m.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 52 features and 9 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -179.1743 ymin: 17.91377 xmax: 179.7739 ymax: 71.35256
## Geodetic CRS:  NAD83
#print(US [, c("NAME", "STATEFP")], n = Inf)
US <- US[US$STATEFP !="02" & US$STATEFP != "15" & US$STATEFP != "72", ]
US <- st_transform(US, crs = 5070)
plot(st_geometry(US))

map2016 <- merge(US, count2016, by.x = "NAME", by.y = "State", all.x = TRUE)
map2017 <- merge(US, count2017, by.x = "NAME", by.y = "State", all.x = TRUE)
map2018 <- merge(US, count2018, by.x = "NAME", by.y = "State", all.x = TRUE)
mapTotal <- merge(US, countTotal, by.x = "NAME", by.y = "State", all.x = TRUE)

#Replaces NA with 0
map2016[is.na(map2016$'concerts' ), "concerts"] <- 0
map2017[is.na(map2017$'concerts' ), "concerts"] <- 0
map2018[is.na(map2018$'concerts' ), "concerts"] <- 0
mapTotal[is.na(mapTotal$concerts ), "concerts"] <- 0

head(cbind(map2016$NAME, map2016$concerts, map2017$concerts, map2018$concerts, mapTotal$concerts))
##      [,1]          [,2] [,3] [,4] [,5]
## [1,] "Alabama"     "0"  "0"  "0"  "0" 
## [2,] "Arizona"     "1"  "0"  "0"  "1" 
## [3,] "Arkansas"    "0"  "0"  "0"  "0" 
## [4,] "California"  "3"  "7"  "14" "24"
## [5,] "Colorado"    "0"  "0"  "0"  "0" 
## [6,] "Connecticut" "0"  "0"  "0"  "0"
#head(countTotal)
maptheme <- theme_void() +
  theme(
    plot.title      = element_text(hjust = 0.5, size = 14, face = "bold",
                                   margin = margin(b = 8)),
    plot.subtitle   = element_text(hjust = 0.5, size = 10, color = "green",
                                   margin = margin(b = 6)),
    legend.position = "bottom",
    legend.title    = element_text(size = 11),
    legend.text     = element_text(size = 8),
    plot.margin     = margin(10, 10, 10, 10)
  )
maxY <- max(map2016$'2016', map2017$'2017', map2018$'2018')
plot2016 <- ggplot(map2016) +
  geom_sf(aes(fill = concerts), color = 'black', size = 0.5) +
  scale_fill_distiller(palette = "YlOrRd", name = "Concerts") +
  labs(title = "Internet concerts by State in 2016") +
  maptheme

plot2016

plot2017 <- ggplot(map2017)+
  geom_sf(aes(fill= concerts), color = 'black', size=0.5)+
  scale_fill_distiller(palette = "Purples", name = "Concerts") +
  labs(title = "Internet concerts by State in 2017") +
  maptheme
plot2017

plot2018 <- ggplot(map2018)+
  geom_sf(aes(fill= concerts), color = 'black', size=0.5)+
  scale_fill_distiller(palette = "Blues", name = "Concerts") +
  labs(title = "Internet concerts by State in 2018") +
  maptheme
plot2018

plotTotal <- ggplot(mapTotal)+
  geom_sf(aes(fill= concerts), color = 'black', size=0.5)+
  scale_fill_distiller(palette = "RdPu", name = "Concerts") +
  labs(title = "Internet concerts by State(2016-2018)") +
  maptheme
plotTotal

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.