library(tuichartr)
library(gapminder)
library(dplyr)
library(tidyr)

Bar / column charts

Simple columns

# 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)

Dodge columns

# 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)

Stack columns

# 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")

Horizontal bars

# 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)

Lines

One serie

# 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)

Several series

# 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")

Scatter & bubbles

Basic scatter

# 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)

Group scatter

# 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")

Bubbles

# 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")

Heatmap

# 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"
    )
  )

Treemap

# 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")

Boxplot

# 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)

Radial

# 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)
  )

Pie / donut

Half donut: