r_gapminder Flashcards

1
Q

Show the “country” and “infant_mortality” for the following from the gapminder dataset:

  • year == 2015
  • country == Sri Lanka and Turkey
A

gapminder %>% +

filter(year == 2015 & country %in% c(“Sri Lanka”, “Turkey”)) %>% +

select(country, infant_mortality)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Use faceting and ggplot to create a scatter plot with the following:

  • filter on “gapminder” dataset and “years” 1962 and 2012
  • plot “fertility” and “life_expectancy”, with colored “continent” labels
  • facet_grid with “continent” in the rows and “year” in the columns
A

filter(gapminder, year%in%c(1962, 2012) ) %>% +

ggplot( aes( fertility, life_expectancy, col = continent) ) +

geom_point() +

facet_grid( continent~year )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Use faceting and ggplot to create a scatter plot with the following:

  • filter on “gapminder” dataset and “years” 1962 and 2012
  • plot “fertility” and “life_expectancy”, with colored “continent” labels
  • facet_grid with “year” in the columns
A

filter(gapminder, year%in%c(1962, 2012) ) %>% +

ggplot( aes( fertility, life_expectancy, col = continent) ) +

geom_point() +

facet_grid( .~year )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are Time Series plots?

A
  • Plots with:
    • time in the x-axis
    • an outcome or measurement on the y-axis
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Create a time series plot with the following:

  • Gapminder dataset
  • Filter on “country” of United States
  • Plot “year” on the x-axis and “fertility” on the y-axis
  • Use a geom_line
A

gapminder %>% filter(country == “United States”) %>% +

ggplot( aes( year, fertility) ) +

geom_line()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a key step you must take when comparing two countries in time series plots?

A
  • By default, a line will go through the points for both countries
  • To let ggplot know we want two separate lines, we assign each point to a group (one for each country)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create a time series plot for two countries (South Korea, Germany), and plot on “year” and “fertility”

A

countries <- c(“South Korea”, “Germany”)

gapminder %>% filter(country %in% countries) %>% +

ggplot(aes (year, fertility, color = countries) ) +

geom_line()

Note: the use of “color” has two effects:

  • same as “group = countries”
  • also colors the countries in the group by different colors and inserts a legend
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create a time series with the following:

  • labels
    • country = countries
    • x = 1975, 1965
    • y = 60, 72
  • Use gapminder dataset
  • filter on country
  • No legend
  • for geom_text
    • use size of 5
    • label = country
A

labels <- data.frame(country = countries, x = c(1975, 1965), y = c(60, 72))

gapminder %>% filter(country %in% countries) %>% +

ggplot( aes( year, life_expectancy, col = country) ) +

geom_line() +

geom_text( data = labels, aes( x, y, label = country) , size = 5) +

them(legend.position = “none”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the mode of a normal distribution?

A
  • the average
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are local modes?

A
  • Points on a normal distribution where the distiribution goes down and up again
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Use the gapminder dataset to do the following:

  • Filter on “past_year” from year and no NA in gdp
  • Plot dollars_per_day
  • Histogram with bin values of ‘1’ and color black
  • Transform the scale to base 2
A

gapminder %>% +

filter(year == “past_year”, !no.na(gdp) ) +

ggplot(dollars_per_day) +

geom_histogram(binwidth=1, color=black) +

scale_x_continuous(trans=”log2”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Take the existing dataset ‘p’ and adjust as follows:

  • Change to box plot
  • Rotate the x-axis labels 90 degrees
A

p + geom_plot() +

theme(axis.text.x = element_text(angle = 90, hjust = 1) )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Use the gapminder dataset to do the following:

  • Filter on “past_year” from year and no NA in gdp
  • Create a “region” variable that orders the plot on “region” and “dollars per day” with the median function
  • Plot “region”, “dollars_per_day” with fill on “continent”
  • Box plot
  • Rotate the x-axis labels 90 degrees
  • xlab
A

gapminder %>% +

filter( year==”pas_year”, !is.na(gdp) %>% +

How well did you know this?
1
Not at all
2
3
4
5
Perfectly