사용자 지정 코그노스틱 추가
사용자 지정 코그노스틱을 만들어 봅시다. 이를 위해 gapminder 데이터에 delta_lifeExp와 ihme_link라는 두 개의 새 변수를 추가할 거예요.
이 연습은 강의의 일부입니다
R에서 Trelliscope로 빅데이터 시각화하기
연습 안내
- 그룹화 변수인
country별로 첫 번째와 마지막 관측 기대수명(lifeExp)의 차이를 계산해, gapminder 데이터에delta_lifeExp라는 새 변수를 추가하세요. 데이터는 이미 연도 기준으로 정렬되어 있어요. - 또 다른 변수
ihme_link를 추가해 healthdata.org의 각 국가 프로필로 연결되도록 하세요.country값의 공백을 대시로 바꾸기 위해space_to_dash()를 사용합니다. 예를 들어, "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"))