Comparison of Life Exectancy by Country Presented on a World Map

world_map <- map_data("world")
ggplot(world_map, aes(x = long, y = lat, group = group)) +
  geom_polygon(fill="lightblue", colour = "white")

str(world_map)
## 'data.frame':    99338 obs. of  6 variables:
##  $ long     : num  -69.9 -69.9 -69.9 -70 -70.1 ...
##  $ lat      : num  12.5 12.4 12.4 12.5 12.5 ...
##  $ 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  "Aruba" "Aruba" "Aruba" "Aruba" ...
##  $ subregion: chr  NA NA NA NA ...
world_map$Country <- world_map$region
#names(world_map)
world_map$Country <- factor(world_map$Country)
#levels(world_map$Country)
Life.Exp <- read.csv("~/Mscs 150 I20/Class/Data/Life Expectancy Data.csv")
#names(Life.Exp)
#str(Life.Exp)
#levels(Life.Exp$Country)

Some of the Country levels did not match. I’ve changed a few here but not all.

Life.Exp$Country=recode(Life.Exp$Country,"United Kingdom of Great Britain and Northern Ireland"="UK")
Life.Exp$Country=recode(Life.Exp$Country,"United States of America"="USA")
Life.Exp$Country=recode(Life.Exp$Country,"Russian Federation"="Russia")
#levels(Life.Exp$Country)

Then, I used inner_join() instead of left_join() which dropped nonmatches rather than keeping them with NA for life.expectancy.

Life.latlong2 <- inner_join(world_map, Life.Exp, by="Country")
## Warning: Column `Country` joining factors with different levels, coercing
## to character vector
#head(Life.latlong2)
#names(Life.latlong2$Country)

First, I graphed with a color fill by country.

p <- ggplot(data = Life.latlong2, aes(x=long, y=lat, group=group, fill=Country))
p + geom_polygon(color = "gray50") + theme(legend.position="none")

First I graphed with a color fill by the variable life expectancy with a gradient scale.

p <- ggplot(data = Life.latlong2, aes(x=long, y=lat, group=group,
                                      fill=Life.expectancy))
p + geom_polygon(color = "gray50") +
  scale_fill_gradient(low="white", high ="blue") +
  theme(legend.position="top")