knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(tuichartr) library(gapminder) library(dplyr) library(tidyr)
# Datas simple_cols <- gapminder_unfiltered %>% filter(year == 2007) %>% count(continent) # Chart tuichart("column") %>% add_data(simple_cols, aes(x = continent, y = n)) %>% tui_chart(title = "Countries by continent in 2007") %>% tui_yAxis(title = "Number of countries") %>% tui_legend(visible = FALSE) %>% tui_series(showLabel = TRUE)
# Datas dodge_cols <- gapminder_unfiltered %>% filter(year %in% c(1950, 2007)) %>% count(continent, year) %>% complete(continent, year) # Chart tuichart("column") %>% add_data(dodge_cols, aes(x = continent, y = n, group = year)) %>% tui_chart(title = "Countries by continent: 1950 vs 2007") %>% tui_yAxis(title = "Number of countries") %>% tui_series(showLabel = TRUE)
# Datas stacked_cols <- gapminder_unfiltered %>% filter(year == 2007) %>% mutate(meanLifeExp = if_else(lifeExp >= mean(lifeExp), "above mean", "under mean")) %>% count(continent, meanLifeExp) %>% complete(continent, meanLifeExp) # Chart tuichart("column") %>% add_data(stacked_cols, aes(x = continent, y = n, group = meanLifeExp)) %>% tui_chart(title = "Countries by continent: position in relation to average life expectancy") %>% tui_yAxis(title = "Number of countries") %>% tui_tooltip(grouped = TRUE) %>% tui_series(stackType = "normal")
# Datas horiz_bars <- gapminder %>% filter(continent == "Americas", year == 2007) %>% arrange(lifeExp) # Chart tuichart("bar") %>% add_data(horiz_bars, aes(x = country, y = lifeExp)) %>% tui_chart(title = "Life expectancy in America") %>% tui_xAxis(title = "Life expectancy in 2007") %>% tui_legend(visible = FALSE)
# Datas line_one <- gapminder %>% filter(country == "Nigeria") # Chart tuichart("line") %>% add_data(line_one, aes(x = year, y = lifeExp)) %>% tui_chart(title = "Life expectancy in Nigeria") %>% tui_yAxis(title = "Life expectancy evolution") %>% tui_legend(visible = FALSE)
# Datas lines_mult <- gapminder %>% filter(country %in% c("Nigeria", "Cameroon")) %>% mutate(country = droplevels(country)) # Chart tuichart("line") %>% add_data(lines_mult, aes(x = year, y = lifeExp, group = country)) %>% tui_chart(title = "Life expectancy in Nigeria & Cameroon") %>% tui_yAxis(title = "Life expectancy evolution") %>% tui_legend(visible = TRUE, align = "bottom")
# Datas scatter <- gapminder %>% filter(year == 2007) # Chart tuichart("scatter") %>% add_data(scatter, aes(x = gdpPercap, y = lifeExp, label = country)) %>% tui_chart(title = "Life expectancy X GDP per capita") %>% tui_yAxis(title = "Life expectancy") %>% tui_xAxis(title = "GDP per capita") %>% tui_legend(visible = FALSE)
# Chart tuichart("scatter") %>% add_data(scatter, aes(x = gdpPercap, y = lifeExp, group = continent, label = country)) %>% tui_chart(title = "Life expectancy X GDP per capita") %>% tui_yAxis(title = "Life expectancy") %>% tui_xAxis(title = "GDP per capita") %>% tui_legend(visible = TRUE, align = "top")
# Chart tuichart("bubble") %>% add_data( scatter %>% filter(continent %in% c("Europe", "Oceania")), aes(x = gdpPercap, y = lifeExp, group = continent, label = country, size = pop) ) %>% tui_yAxis(title = "Life expectancy") %>% tui_xAxis(title = "GDP per capita") %>% tui_legend(visible = TRUE, align = "bottom")
# Datas heatmap <- gapminder %>% filter(country %in% sample(country, 8)) # Chart tuichart("heatmap") %>% add_data( data = heatmap, mapping = aes(x = year, y = country, value = gdpPercap) ) %>% tui_chart(title = "GDP over time for 8 random countries")
Changing colors:
# Datas heatmap <- gapminder %>% filter(country %in% sample(country, 8)) # Chart tuichart("heatmap") %>% add_data( data = heatmap, mapping = aes(x = year, y = country, value = gdpPercap) ) %>% tui_chart(title = "GDP over time for 8 random countries") %>% tui_theme( series = list( startColor = "#DEEBF7", endColor = "#084594" ) )
level2
aesthetic is optional.
# Datas treemap <- gapminder %>% filter(year == 2007) %>% filter(pop > quantile(pop, 3/4)) # Chart tuichart("treemap") %>% add_data( data = treemap, mapping = aes(level1 = continent, level2 = country, value = pop) ) %>% tui_series( showLabel = TRUE, zoomable = FALSE, useLeafLabel = TRUE ) %>% tui_chart(title = "Most populated countries per continent")
With drilldown:
tuichart("treemap") %>% add_data( data = treemap, mapping = aes(level1 = continent, level2 = country, value = pop) ) %>% tui_series( showLabel = TRUE, zoomable = TRUE, useLeafLabel = FALSE ) %>% tui_chart(title = "Most populated countries per continent")
# Chart tuichart("boxplot") %>% add_data(filter(gapminder, year == 2007), aes(x = continent, y = lifeExp)) %>% tui_chart(title = "Life expectancy distribution per continent") %>% tui_legend(visible = FALSE)
With grouping variable
# Chart tuichart("boxplot") %>% add_data(filter(gapminder, year %in% c(1952, 2007)), aes(x = continent, y = lifeExp, group = year)) %>% tui_chart(title = "Life expectancy distribution per continent") %>% tui_legend(visible = TRUE)
# Datas radial <- gapminder %>% filter(country == "Chile") # Chart tuichart("radial") %>% add_data( data = radial, mapping = aes(x = year, y = gdpPercap) )
# Datas radial <- gapminder %>% filter(country %in% c("Chile", "Argentina")) # Chart tuichart("radial") %>% add_data( data = radial, mapping = aes(x = year, y = gdpPercap, group = country) )
# Datas pie <- gapminder %>% filter(year == 2007) %>% count(continent) # Chart tuichart("pie") %>% add_data( data = pie, mapping = aes(x = continent, y = n) ) %>% tui_series( showLegend = TRUE, showLabel = TRUE, labelAlign = "center" ) %>% tui_legend(visible = FALSE) %>% tui_theme( series = list(label = list(color = "#FFF")) )
Half donut:
# Chart tuichart("pie") %>% add_data( data = pie, mapping = aes(x = continent, y = n) ) %>% tui_series( startAngle = -90, endAngle = 90, radiusRange = c("60%", "100%"), showLegend = TRUE, showLabel = TRUE, labelAlign = "outer" ) %>% tui_legend(visible = FALSE) %>% tui_theme( series = list(label = list(color = "#FFF")) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.