Install and load dplyr
, probably via the tidyverse
meta-package.
library(gapminder)
library(tidyverse)
## ── Attaching packages ──────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 1.0.0 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ─────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(ggridges)
##
## Attaching package: 'ggridges'
## The following object is masked from 'package:ggplot2':
##
## scale_discrete_manual
library(knitr)
Pick three of the six tasks below, and produce:
dplyr
as your data manipulation tool;ggplot2
as your visualization tool; andRelax about the following things:
I have chosen to do Task #2, #3 and #5.
Get the maximum and minimum of GDP per capita for all continents.
min <- gapminder %>%
group_by(continent) %>%
summarize(GDP = min(gdpPercap), value = "min")
max <- gapminder %>%
group_by(continent) %>%
summarize(GDP = max(gdpPercap), value = "max")
table <- rbind(min,max) %>% arrange(continent)
plot <- table %>%
group_by(value) %>%
ggplot(aes(continent,GDP, fill = value)) +
geom_col(position="dodge") +
scale_fill_discrete("", "Maximum GDP per capita", "Minimum GDP per capita") +
ylab("GDP per capita") +
xlab("Continent") +
ggtitle("Maximum and Minimum GDP per capita") +
theme(text = element_text(size=18))
continent | GDP | value |
---|---|---|
Africa | 241.1659 | min |
Africa | 21951.2118 | max |
Americas | 1201.6372 | min |
Americas | 42951.6531 | max |
Asia | 331.0000 | min |
Asia | 113523.1329 | max |
Europe | 973.5332 | min |
Europe | 49357.1902 | max |
Oceania | 10039.5956 | min |
Oceania | 34435.3674 | max |
Look at the spread of GDP per capita within the continents.
table <- gapminder %>%
filter(year > 1980)
plot <- table %>%
ggplot(aes(gdpPercap, continent)) +
geom_density_ridges(scale = 0.9) +
ggtitle("Spread of GDP from 1980-2007") +
theme(text = element_text(size=18)) +
xlab("GDP per capita") +
ylab("Continent")
country | continent | year | lifeExp | pop | gdpPercap |
---|---|---|---|---|---|
Afghanistan | Asia | 1982 | 39.854 | 12881816 | 978.0114 |
Afghanistan | Asia | 1987 | 40.822 | 13867957 | 852.3959 |
Afghanistan | Asia | 1992 | 41.674 | 16317921 | 649.3414 |
Afghanistan | Asia | 1997 | 41.763 | 22227415 | 635.3414 |
Afghanistan | Asia | 2002 | 42.129 | 25268405 | 726.7341 |
Afghanistan | Asia | 2007 | 43.828 | 31889923 | 974.5803 |
Albania | Europe | 1982 | 70.420 | 2780097 | 3630.8807 |
Albania | Europe | 1987 | 72.000 | 3075321 | 3738.9327 |
Albania | Europe | 1992 | 71.581 | 3326498 | 2497.4379 |
How is life expectancy changing over time on different continents?
table <- gapminder %>%
group_by(continent, year) %>%
summarize(avglifeExp = mean(lifeExp))
plot <- table %>%
ggplot(aes(year,avglifeExp, colour = continent)) +
geom_line() +
ggtitle("Mean Life Expectancy from 1952-2007") +
theme(text = element_text(size=18)) +
xlab("Year") +
ylab("Average Life Expectancy")
continent | year | avglifeExp |
---|---|---|
Africa | 1952 | 39.13550 |
Africa | 1957 | 41.26635 |
Africa | 1962 | 43.31944 |
Africa | 1967 | 45.33454 |
Africa | 1972 | 47.45094 |
Africa | 1977 | 49.58042 |
Africa | 1982 | 51.59287 |
Africa | 1987 | 53.34479 |
Africa | 1992 | 53.62958 |
Get table and figure side-by-side.
kable()
due to the .table
tag in the CSS
stylesheet.