library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.3
library(maps)
## Warning: package 'maps' was built under R version 4.1.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.3
##
## 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
MainStates <- map_data("state")
# read the state population data
StatePopulation <- read.csv("https://raw.githubusercontent.com/ds4stats/r-tutorials/master/intro-maps/data/StatePopulation.csv", as.is = TRUE)
str(MainStates)
## 'data.frame': 15537 obs. of 6 variables:
## $ long : num -87.5 -87.5 -87.5 -87.5 -87.6 ...
## $ lat : num 30.4 30.4 30.4 30.3 30.3 ...
## $ group : num 1 1 1 1 1 1 1 1 1 1 ...
## $ order : int 1 2 3 4 5 6 7 8 9 10 ...
## $ region : chr "alabama" "alabama" "alabama" "alabama" ...
## $ subregion: chr NA NA NA NA ...
str(StatePopulation)
## 'data.frame': 50 obs. of 3 variables:
## $ region : chr "alabama" "alaska" "arizona" "arkansas" ...
## $ population : int 4849377 737732 6731484 2994079 38802500 5355856 3596677 935614 19893297 10097343 ...
## $ elect_votes: int 9 3 11 6 55 9 7 3 29 16 ...
ggplot() +
geom_polygon( data=MainStates, aes(x=long, y=lat, group=group),
color="black", fill="lightblue" )

MergedStates <- inner_join(MainStates, StatePopulation, by = "region")
p <- ggplot()
p <- p + geom_polygon( data=MergedStates,
aes(x=long, y=lat, group=group, fill = population/1000000),
color="white", size = 0.2)
p

p <- p + scale_fill_continuous(name="Population(millions)",
low = "lightgreen", high = "darkgreen",limits = c(0,40),
breaks=c(5,10,15,20,25,30,35), na.value = "grey50") +
labs(title="State Population in the Mainland United States")
p

p <- p + guides(fill = guide_colorbar(barwidth = 0.5, barheight = 10,
label.theme = element_text(color = "green", size =10, angle = 45)))
p

AllCounty <- map_data("county")
ggplot() + geom_polygon( data=AllCounty, aes(x=long, y=lat, group=group),
color="darkblue", fill="lightblue", size = .1 ) +
geom_polygon( data=MainStates, aes(x=long, y=lat, group=group),
color="black", fill="lightblue", size = 1, alpha = .3)

p <- p + geom_point(data=us.cities, aes(x=long, y=lat, size = pop)) +
scale_size(name="Population")
us.cities2=arrange(us.cities, pop)
tail(us.cities2)
## name country.etc pop lat long capital
## 1000 Philadelphia PA PA 1439814 40.01 -75.13 0
## 1001 Phoenix AZ AZ 1450884 33.54 -112.07 2
## 1002 Houston TX TX 2043005 29.77 -95.39 0
## 1003 Chicago IL IL 2830144 41.84 -87.68 0
## 1004 Los Angeles CA CA 3911500 34.11 -118.41 0
## 1005 New York NY NY 8124427 40.67 -73.94 0
#plot all states with ggplot
MainCities <- filter(us.cities, long>=-130)
# str(us.cities)
# str(MainCities)
g <- ggplot()
g <- g + geom_polygon( data=MergedStates,
aes(x=long, y=lat, group=group, fill = population/1000000),
color="black", size = 0.2) +
scale_fill_continuous(name="State Population", low = "lightblue",
high = "darkblue",limits = c(0,40), breaks=c(5,10,15,20,25,30,35),
na.value = "grey50") +
labs(title="Population (in millions) in the Mainland United States")
g <- g + geom_point(data=MainCities, aes(x=long, y=lat, size = pop/1000000),
color = "gold", alpha = .5) + scale_size(name="City Population")
g

# Zoom into a particular region of the plot
g <- g + coord_cartesian(xlim=c(-80, -65), ylim = c(38, 46))
g

NewStates <- filter(MainStates,region == "new york" | region ==
"vermont" | region == "new hampshire" | region ==
"massachusetts" | region == "rhode island" |
region == "connecticut" )
g <- g + geom_point(data=MainCities, aes(x=long, y=lat,
size = pop/1000000, color=factor(capital), shape = factor(capital)),
alpha = .5) +
scale_size(name="City Population")
## Scale for 'size' is already present. Adding another scale for 'size', which
## will replace the existing scale.