LoslegenKostenlos loslegen

Adding custom cognostics

Let's create some custom cognostics. To do so, you'll add two new variables to the gapminder data: delta_lifeExp and ihme_link.

Diese Übung ist Teil des Kurses

Visualizing Big Data with Trelliscope in R

Kurs anzeigen

Anleitung zur Übung

  • Add a new variable called delta_lifeExp to the gapminder data that computes the difference between the first and last observed life expectancy (lifeExp) for each country (grouping variable). Note that the data is already sorted by year.
  • Add another variable, ihme_link that links to the country's profile on healthdata.org, using space_to_dash() to replace spaces in country values to dashes. For example, for the country "Costa Rica", the link is "http://www.healthdata.org/Costa-Rica".
  • Give the delta_lifeExp variable a description of "Overall change in life expectancy".
  • Specify default_label = TRUE to make the ihme_link variable be shown as a label by default.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

library(ggplot2)
library(dplyr)
library(gapminder)
library(trelliscopejs)
space_to_dash <- function(x) gsub(" ", "-", x)

# Group by country and create the two new variables
gap <- gapminder %>%
  group_by(___) %>%
  mutate(
    delta_lifeExp = tail(___, 1) - head(___, 1),
    ihme_link = paste0("http://www.healthdata.org/", space_to_dash(___)))

# Add the description
gap$delta_lifeExp <- cog(gap$delta_lifeExp, desc = "___")
# Specify the default label
gap$ihme_link <- cog(gap$ihme_link, default_label = ___)

ggplot(gap, aes(year, lifeExp)) +
  geom_point() +
  facet_trelliscope(~ country + continent,
    name = "lifeExp_by_country",
    desc = "Life expectancy vs. year.",
    nrow = 1, ncol = 2,
    scales = c("same", "sliced"))
Code bearbeiten und ausführen