## load libraries
library(dplyr) # you need to have all libraries loaded within the Rmd file
library(ggplot2) #install.packages('ggplot2')
library(gapminder)
# get range of available data
summary(gapminder)
# setup dataframe
g = gapminder %>%
filter(year==2007) %>% # most recent year
mutate(pop_m = pop/1e6) # population, millions
# plot scatterplot of most recent year
s = ggplot(g, aes(x=gdpPercap, y=lifeExp)) +
geom_point()
s
# add aesthetic of size by population
s = s +
aes(size=pop_m)
s
# add aesthetic of color by continent
s = s +
aes(color=continent)
s
# add title, update axes labels
s = s +
ggtitle('Health & Wealth of Nations for 2007') +
xlab('GDP per capita ($/year)') +
ylab('Life expectancy (years)')
s
# label legend
s = s +
scale_colour_discrete(name='Continent') +
scale_size_continuous(name='Population (M)')
s
Your Turn: Make a similar plot but for gdpPercap
. Be sure to update the plot’s aesthetic, axis label and title accordingly.
# boxplot by continent
b = ggplot(g, aes(x=continent, y=lifeExp)) +
geom_boxplot()
b
# match color to continents, like scatterplot
b = b +
aes(fill=continent)
b
# drop legend, add title, update axes labels
b = b +
theme(legend.position='none') +
ggtitle('Life Expectancy by Continent for 2007') +
xlab('Continent') +
ylab('Life expectancy (years)')
b
plotly
library(plotly) # install.packages('plotly')
# scatterplot (Note: key=country shows up on rollover)
s = ggplot(g, aes(x=gdpPercap, y=lifeExp, key=country)) +
geom_point()
ggplotly(s) # must comment out or have eval=FALSE to knit
# boxplot
ggplotly(b) # must comment out or have eval=FALSE to knit
library(tmap) # install.packages('tmap')
# load world spatial polygons
data(World)
# inspect values in World
World@data %>% tbl_df()
# gapminder countries not in World. skipping for now
g %>%
anti_join(World@data, by=c('country'='name')) %>%
arrange(desc(pop))
# World countries not in gapminder. skipping for now
World@data %>%
anti_join(g, by=c('name'='country')) %>%
arrange(desc(pop_est)) %>%
select(iso_a3, name, pop_est)
# join gapminder data to World
World@data = World@data %>%
left_join(g, by=c('name'='country'))
# make map
m = tm_shape(World) +
tm_polygons('lifeExp', palette='RdYlGn', id='name', title='Life expectancy (years)', auto.palette.mapping=F) +
tm_style_gray() + tm_format_World()
m
# show interactive map
tmap_leaflet(m)