ggplot(oceania, # input the dataaes(x = year, y = gdpPercap, color = country,linetype = country)) +# establish aesthetic mappingsgeom_line(size =1) +# apply mapping to geom objectsggtitle("Life expectancy in Oceanian countries over time") +# add titlelabs(x ="Year", y ="GDP per capita") +# add labelstheme_bw() # change background to white background with grey gridlines
ggplot(oceania, # input the dataaes(x = year, y = gdpPercap, color = country,linetype = country)) +# establish aesthetic mappingsgeom_line(size =1) +# apply mapping to geom objectsggtitle("Life expectancy in Oceanian countries over time") +# add titlelabs(x ="Year", y ="GDP per capita") +# add labelstheme_bw() # change background to white background with grey gridlines
ggplot(oceania, # input the dataaes(x = year, y = gdpPercap, color = country,linetype = country)) +# establish aesthetic mappingsgeom_line(size =1) +# apply mapping to geom objectsgeom_vline(xintercept =1992, linetype ="dashed") +# add a dashed line indicating a faster rise in gdp percapscale_y_continuous(labels = scales::label_dollar(negative_parens = T)) +# display a finance style for y-axis labelsggtitle("Life expectancy in Oceanian countries over time") +# add titlelabs(x ="Year", y ="GDP per capita") +# add labels for x and y axestheme_bw() +# change background to white background with grey gridlines theme(plot.title =element_text(hjust =0.5)) +# center the plot titleguides(color =guide_legend("Country"), linetype ="none") # capitalize the legend title
# show frequencies of a variablegapminder %>%filter(year ==1952) %>%ggplot(aes(x = lifeExp)) +geom_histogram(binwidth =2) +theme_light() +labs(x ="Life Expectancy", y ="Count", title ="Life Expectancy in 1952")
# show frequencies of a variablegapminder %>%filter(year ==1952) %>%ggplot(aes(x = lifeExp)) +geom_density(size =1.5, alpha =0.2, fill ="red") +theme_light() +labs(x ="Life Expectancy", y ="Count", title ="Life Expectancy in 1952")
# show distribution of a variable (median, 1st, 3rd quantiles, outliers)gapminder %>%filter(year ==1952, continent=="Europe") %>%ggplot(aes(y = lifeExp)) +geom_boxplot(fill ="grey", color ="blue", outlier.shape =1) +# adjust aestheticstheme_light() +labs(title ="Life Expectancy in 1952 (Europe)", y ="Life Expectancy", x ="")
# show distribution of a discrete variable ggplot(gapminder, aes(x = continent,fill = continent)) +# differentiate the filled colorsgeom_bar() +theme_classic() +labs(y ="Number of countries", x ="Continent")
gapminder %>%filter(year >1990) %>%group_by(year, continent) %>%summarise(totalpop =sum(as.double(pop))) %>%ggplot(aes(x = year, y = totalpop, fill = continent)) +geom_col(position ="dodge", size =0.2, alpha =0.8) +# dodge overlapping objects side by side scale_x_continuous(breaks =seq(1992, 2007, 5), expand =c(0, 0)) +scale_y_continuous(labels = scales::comma, expand =c(0, 0)) +scale_fill_brewer(palette ="Set1") +theme_bw()
Practices
Exercise 1: Make a scatter plot for annual average GDP per capita across all countries.
Exercise 2: Break down the plot from exercise 1 by continent, using colors to distinguish the points and transforming mean GDP per capita on the log scale.
Exercise 3: Make a collection of bar plots faceted by year that compare mean GDP per capita across countries in a given year. Orient the plots to make it easier to read the continent labels.
Exercise 4: What is the relationship between life expectancy and GDP per capita in 2007 by non-Oceanian continents?
gapminder %>%group_by(year, continent) %>%# aggregate the information by year by continentsummarize(meanGDPpc =mean(gdpPercap)) %>%ggplot(aes(x = year, y = meanGDPpc, color = continent)) +geom_point() +scale_y_log10() # apply the log scale to GDP per capita
gapminder %>%group_by(year, continent) %>%summarize(meanGDPpc =mean(gdpPercap)) %>%ggplot(aes(x = continent, y = meanGDPpc)) +geom_col() +facet_wrap(~ year) +coord_flip() # flip the coordinates so that the continent names are visible
ggplot(dc_tes) + # input data on tree equity score in DC geom_sf(aes(fill = tes), color = "#e7e5cc") + # draw polygons with colors representing tree equity scores (tes) scale_fill_continuous(low = "#c2d6a4", high = "#1e3d14", na.value = "grey90", name= "Tree Equity Score") + # adjust aesthetics of polygons geom_sf(data = dc_gunshot, color = "#c62320", size = 0.1) + # add points representing gunshot incidents coord_sf(xlim = c(-77.12, -76.90), ylim = c(38.79, 39.01), expand = F) + # remove empty space on the map labs(x = "", y = "", title = "GUNSHOT DETECTION MAP", subtitle = "Recorded shooting incidents in Washington D.C. during 2021", caption = str_wrap("Data comes from ShotSpotter gunshot detection system. Incidents of probable gunfires and firecrackers are excluded. Green spaces represent tree equity scores which compute how much tree canopy and surface temperature align with income, employment, race, age and health factors at each block | Visualization by Zhaowen Guo", width = 200))