Beautiful interactive visualizations
The package highcharacter allows to build nice interactive plots and may be viewed as the best solution for displayed some changes in time.
In this case, the gapminder dataset is used and changes in population size are shown.
The example is based on the following book chapter.
Rob Kabacoff, Data Visualization with R. 2020.
Uploading data
What it is about
## country
## [1,] "Afghanistan"
## [2,] "Albania"
## [3,] "Algeria"
## [4,] "Angola"
## [5,] "Argentina"
## [6,] "Australia"
## continent
## [1,] "Africa"
## [2,] "Americas"
## [3,] "Asia"
## [4,] "Europe"
## [5,] "Oceania"
## 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 2002 2007
## 142 142 142 142 142 142 142 142 142 142 142 142
library(psych)
library(kableExtra)
library(tidyverse)
describe(df) %>%
round(digits = 0) %>%
knitr::kable() %>%
kable_styling()| vars | n | mean | sd | median | trimmed | mad | min | max | range | skew | kurtosis | se | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| country* | 1 | 1704 | 72 | 41 | 72 | 72 | 53 | 1 | 142 | 141 | 0 | -1 | 1 |
| continent* | 2 | 1704 | 2 | 1 | 2 | 2 | 1 | 1 | 5 | 4 | 0 | -1 | 0 |
| year | 3 | 1704 | 1980 | 17 | 1980 | 1980 | 22 | 1952 | 2007 | 55 | 0 | -1 | 0 |
| lifeExp | 4 | 1704 | 59 | 13 | 61 | 60 | 16 | 24 | 83 | 59 | 0 | -1 | 0 |
| pop | 5 | 1704 | 29601212 | 106157897 | 7023596 | 11399459 | 7841474 | 60011 | 1318683096 | 1318623085 | 8 | 78 | 2571683 |
| gdpPercap | 6 | 1704 | 7215 | 9857 | 3532 | 5221 | 4008 | 241 | 113523 | 113282 | 4 | 27 | 239 |
## The following data contains information about 142 countries in five continents
## The period of observations is between 1952 and 2007 (with five years interval)
## It includes such indexes as population, life expectancy, and GDP Interactive plots
The highcharter package allows to create nicely formatted interactive plots. In this case, the graph is about population changes in six Asian countries from 1952 to 2007.
## install.packages("highcharter")
library(highcharter)
## Remaining only population size in Asian countries
asia <- filter(df, continent == "Asia")
asia <- select(asia, year, country, pop)
## Convert to long to wide format
library(tidyr)
sprasia <- spread(asia, country, pop)## Define six Asian countries and plot draft plot
## Years are on the X-axis, population - Y-axis, and countries are represented with six different lines from blue to red
graph <- highchart() %>%
hc_xAxis(categories = sprasia$year) %>%
hc_add_series(name = "China",
data = sprasia$China) %>%
hc_add_series(name = "India",
data = sprasia$India) %>%
hc_add_series(name = "Japan",
data = sprasia$Japan) %>%
hc_add_series(name = "Iran",
data = sprasia$Iran) %>%
hc_add_series(name = "Syria",
data = sprasia$Syria) %>%
hc_add_series(name = "Pakistan",
data = sprasia$Pakistan)
graphImproved version:
graph <- graph %>%
## assinging title to the plot and determing its style
hc_title(text = "Population changes by Country",
margin = 20,
align = "left",
style = list(color = "steelblue")) %>%
## the same with subtitle
hc_subtitle(text = "1952 to 2007",
align = "left",
style = list(color = "#2b908f",
fontWeight = "bold")) %>%
## add link to gapminder dataset in the caption section
hc_credits(enabled = TRUE, # add credits
text = "Gapminder Data",
href = "http://gapminder.com") %>%
## legend is put on the upper left corner
hc_legend(align = "left",
verticalAlign = "top",
layout = "vertical",
x = 0,
y = 100) %>%
## this line adds the interactivity - joint small legend for any point on the chart
hc_tooltip(crosshairs = TRUE,
backgroundColor = "#FCFFC5",
shared = TRUE,
borderWidth = 4) %>%
## just enables to download the chart, may be avoided
hc_exporting(enabled = TRUE)
graph## Moreover, by clicking on the legend in the upper left corner, it is possible to specify countries or remain only one and examine it in detail.
## In sum, it seems that population in all of the given countries was on the increase - but in some it was stable, while in some more of a fluctuating nature (e.g. Iran).