开始使用免费开始使用

添加自定义 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"))
编辑并运行代码