開始使用免費開始

加入自訂 cognostics

來建立一些自訂的 cognostics。你將在 gapminder 資料中加入兩個新變數:delta_lifeExpihme_link

本練習屬於課程

使用 R 的 Trelliscope 視覺化巨量資料

檢視課程

練習說明

  • 在 gapminder 資料中加入名為 delta_lifeExp 的新變數,對每個 country(分組變數)計算其最早與最新觀察到的平均壽命(lifeExp)之差。注意資料已依年份排序。
  • 再加入另一個變數 ihme_link,連到該國在 healthdata.org 的個人頁面,並使用 space_to_dash()country 值中的空格換成連字號。例如,對於國家「Costa Rica」,連結為「http://www.healthdata.org/Costa-Rica」。
  • delta_lifeExp 變數的說明設為 "Overall change in life expectancy"
  • 指定 default_label = TRUE,讓 ihme_link 變數預設以標籤顯示。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

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"))
編輯並執行程式碼