Skip to content
gapminder, ggplot
  • AI Chat
  • Code
  • Report
  • Spinner

    Introduction to the Tidyverse

    Run the hidden code cell below to import the data used in this course.

    # Load the Tidyverse
    library(tidyverse)
    
    # Read in the gapminder file
    gapminder <- read.csv("datasets/gapminder.csv", sep = "\t")

    Take Notes

    Add notes about the concepts you've learned and code cells with code you want to keep.

    Add your notes here

    # Add your code snippets here
    ggplot(gapminder_1952, aes(x = pop, y = lifeExp)) +
      geom_point() + scale_x_log10()
    
    ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) +
      geom_point() + scale_x_log10() + facet_wrap(~ year)
    
    gapminder %>%
    filter(year == 1957) %>%
    group_by(continent) %>%
    summarize(medianLifeExp = median(lifeExp), maxGdpPercap = max(gdpPercap))
    
    # Summarize medianGdpPercap within each continent within each year: by_year_continent
    by_year_continent <- gapminder %>%
      group_by(continent, year) %>%
      summarize(medianGdpPercap = median(gdpPercap))
    
    # Plot the change in medianGdpPercap in each continent over time
    ggplot(by_year_continent, aes(x = year, y = medianGdpPercap, color = continent)) +
      geom_point() +
      expand_limits(y = 0)
    
    # Summarize the median gdpPercap by year, then save it as by_year
    by_year <- gapminder %>%
      group_by(year) %>%
      summarize(medianGdpPercap = median(gdpPercap))
    
    
    # Create a line plot showing the change in medianGdpPercap by continent over time
    ggplot(by_year_continent, aes(x = year, y = medianGdpPercap, color = continent)) + 
      geom_line() +
      expand_limits(y = 0)
    
    # Create a histogram of population (pop), with x on a log scale
    ggplot(gapminder_1952, aes(x = pop)) +
      geom_histogram(bins = 50) + scale_x_log10()
    
    # Create a boxplot comparing gdpPercap among continents
    
    ggplot(gapminder_1952, aes(x = continent, y = gdpPercap)) +
      geom_boxplot() + scale_y_log10()
    
    # Add a title to this graph: "Comparing GDP per capita across continents"
    ggplot(gapminder_1952, aes(x = continent, y = gdpPercap)) +
      geom_boxplot() +
      scale_y_log10()+
      ggtitle("Comparing GDP per capita across continents")