2019-09-04 12:55:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
library(apexcharter)
|
|
|
|
library(dplyr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Bar ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
data("mpg", package = "ggplot2")
|
|
|
|
|
|
|
|
count(mpg, class) %>%
|
2019-09-05 10:48:19 +02:00
|
|
|
mutate(pct = n/sum(n)) %>%
|
2019-09-04 12:55:13 +02:00
|
|
|
apex(mapping = aes(class, pct), type = "column") %>%
|
|
|
|
ax_colors("#617a89") %>%
|
|
|
|
ax_labs(
|
|
|
|
title = "Seminal ggplot2 column chart example with percents",
|
|
|
|
subtitle = "Example taken from {hrbrthemes} readme"
|
|
|
|
) %>%
|
|
|
|
ax_yaxis(
|
|
|
|
title = list(text = "Weight (tons)"),
|
|
|
|
labels = list(
|
2019-09-05 10:48:19 +02:00
|
|
|
formatter = JS(
|
|
|
|
"function(value) {return Math.round(value * 100) + '%';}"
|
|
|
|
)
|
2019-09-04 12:55:13 +02:00
|
|
|
),
|
|
|
|
tickAmount = 6
|
|
|
|
) %>%
|
2019-09-05 10:48:19 +02:00
|
|
|
ax_tooltip(
|
|
|
|
y = list(
|
|
|
|
title = list(
|
|
|
|
formatter = JS("function() {return 'Percentage';}")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) %>%
|
2019-09-04 12:55:13 +02:00
|
|
|
ax_xaxis(
|
|
|
|
title = list(text = "Fuel efficiency (mpg)")
|
|
|
|
) %>%
|
|
|
|
ax_title(
|
|
|
|
style = list(fontSize = "22px")
|
|
|
|
) %>%
|
|
|
|
ax_subtitle(
|
|
|
|
style = list(fontSize = "16px", color = "#BDBDBD")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Line --------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2019-09-05 11:27:44 +02:00
|
|
|
data("unhcr_ts")
|
|
|
|
|
|
|
|
unhcr_ts %>%
|
|
|
|
filter(population_type == "Refugees (incl. refugee-like situations)") %>%
|
|
|
|
mutate(date = as.Date(paste0(year, "-01-01"))) %>%
|
|
|
|
apex(aes(date, n, group = continent_origin), type = "line") %>%
|
2019-09-04 12:55:13 +02:00
|
|
|
ax_legend(position = "bottom") %>%
|
2019-09-05 11:27:44 +02:00
|
|
|
ax_stroke(width = 2) %>%
|
|
|
|
ax_colors("#440154", "#414487", "#2A788E", "#22A884", "#7AD151",
|
|
|
|
"#FDE725") %>%
|
2019-09-04 12:55:13 +02:00
|
|
|
ax_yaxis(
|
|
|
|
labels = list(
|
2019-09-05 11:27:44 +02:00
|
|
|
formatter = htmlwidgets::JS("function(val) {return (val/1e6).toFixed(0);}")
|
|
|
|
),
|
|
|
|
title = list(text = "Number of refugees (in million)")
|
|
|
|
) %>%
|
|
|
|
ax_xaxis(type = "datetime", labels = list(format = "yyyy")) %>%
|
|
|
|
ax_annotations(
|
|
|
|
points = list(
|
|
|
|
list(
|
|
|
|
x = JS("new Date('1994').getTime()"),
|
|
|
|
y = 6935296,
|
|
|
|
label = list(text = "Great Lakes refugee crisis", offsetY = 0),
|
|
|
|
marker = list(size = 6)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) %>%
|
|
|
|
ax_tooltip(
|
|
|
|
x = list(format = "yyyy"),
|
|
|
|
y = list(
|
|
|
|
formatter = JS(
|
|
|
|
# thousand separator in javascript
|
|
|
|
"function(value) {return value.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\");}"
|
|
|
|
)
|
2019-09-05 10:48:19 +02:00
|
|
|
)
|
2019-09-04 12:55:13 +02:00
|
|
|
) %>%
|
|
|
|
ax_labs(
|
2019-09-05 11:27:44 +02:00
|
|
|
title = "Continent of origin for refugees population",
|
|
|
|
subtitle = "Data from the UN Refugee Agency"
|
2019-09-04 12:55:13 +02:00
|
|
|
) %>%
|
|
|
|
ax_title(
|
|
|
|
style = list(fontSize = "22px")
|
|
|
|
) %>%
|
|
|
|
ax_subtitle(
|
|
|
|
style = list(fontSize = "16px", color = "#BDBDBD")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Scatter -----------------------------------------------------------------
|
|
|
|
|
|
|
|
data("gapminder", package = "gapminder")
|
|
|
|
|
|
|
|
|
|
|
|
gapminder %>%
|
|
|
|
filter(year == 2007) %>%
|
|
|
|
mutate(
|
|
|
|
gdpPercap = log(gdpPercap),
|
2019-09-05 11:27:44 +02:00
|
|
|
pop = sqrt(pop / pi) / 1500
|
2019-09-04 12:55:13 +02:00
|
|
|
) %>%
|
|
|
|
apex(mapping = aes(gdpPercap, lifeExp, z = pop, group = continent, label = country), type = "scatter") %>%
|
|
|
|
ax_chart(zoom = list(
|
|
|
|
enabled = TRUE, type = "xy"
|
|
|
|
)) %>%
|
2019-09-05 11:27:44 +02:00
|
|
|
ax_yaxis(
|
|
|
|
decimalsInFloat = 0,
|
|
|
|
axisBorder = list(show = TRUE),
|
|
|
|
axisTicks = list(show = TRUE),
|
|
|
|
title = list(text = "life expectancy at birth (in years)")
|
|
|
|
) %>%
|
|
|
|
ax_xaxis(
|
|
|
|
tickAmount = 8, tooltip = list(enabled = FALSE),
|
|
|
|
title = list(text = "GDP per capita (log-scale)")
|
|
|
|
) %>%
|
2019-09-04 12:55:13 +02:00
|
|
|
ax_grid(xaxis = list(lines = list(show = TRUE))) %>%
|
2019-09-05 11:27:44 +02:00
|
|
|
ax_legend(position = "right", offsetY = 70) %>%
|
2019-09-04 12:55:13 +02:00
|
|
|
ax_markers(hover = list(sizeOffset = 0, size = 25)) %>%
|
|
|
|
ax_tooltip(custom = JS(paste(
|
|
|
|
"function({ series, seriesIndex, dataPointIndex, w }) {",
|
|
|
|
"console.log(w); return (",
|
|
|
|
"'<div>' +",
|
|
|
|
|
|
|
|
"'<div class = \"apexcharts-tooltip-title\">' +",
|
|
|
|
"w.config.series[seriesIndex].data[dataPointIndex].label",
|
|
|
|
"+ '</div>' +",
|
|
|
|
"'<div style = \"padding: 5px;\">' +",
|
|
|
|
"'<div class = \"apexcharts-tooltip-y-group\">' +",
|
|
|
|
|
|
|
|
"'<span class = \"apexcharts-tooltip-text-label\">' +",
|
|
|
|
"'Population: ' +",
|
|
|
|
"'</span>' +",
|
|
|
|
"'<span class = \"apexcharts-tooltip-text-value\">' +",
|
|
|
|
"Math.round(Math.pow(w.config.series[seriesIndex].data[dataPointIndex].z * 1500, 2) * Math.PI).
|
|
|
|
toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") +",
|
|
|
|
"'</span>' +",
|
|
|
|
|
|
|
|
"'</br>' +",
|
|
|
|
|
|
|
|
"'<span class = \"apexcharts-tooltip-text-label\">' +",
|
|
|
|
"'GDP per capita: ' +",
|
|
|
|
"'</span>' +",
|
|
|
|
"'<span class = \"apexcharts-tooltip-text-value\">' +",
|
|
|
|
"Math.round(Math.exp(w.config.series[seriesIndex].data[dataPointIndex].x)) +",
|
|
|
|
"'</span>' +",
|
|
|
|
|
|
|
|
"'</br>' +",
|
|
|
|
|
|
|
|
"'<span class = \"apexcharts-tooltip-text-label\">' +",
|
|
|
|
"'Life expectancy: ' +",
|
|
|
|
"'</span>' +",
|
|
|
|
"'<span class = \"apexcharts-tooltip-text-value\">' +",
|
|
|
|
"w.config.series[seriesIndex].data[dataPointIndex].y +",
|
|
|
|
"'</span>' +",
|
|
|
|
|
|
|
|
|
|
|
|
"'</div>' +",
|
|
|
|
"'</div>' +",
|
|
|
|
|
|
|
|
"'</div>'",
|
|
|
|
");",
|
|
|
|
"}", sep = "\n"
|
2019-09-05 11:27:44 +02:00
|
|
|
))) %>%
|
|
|
|
ax_labs(
|
|
|
|
title = "Life expectancy, GDP and population",
|
|
|
|
subtitle = "gapminder dataset from {gapminder}"
|
|
|
|
) %>%
|
|
|
|
ax_title(
|
|
|
|
style = list(fontSize = "22px")
|
|
|
|
) %>%
|
|
|
|
ax_subtitle(
|
|
|
|
style = list(fontSize = "16px", color = "#BDBDBD")
|
|
|
|
)
|
2019-09-04 12:55:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Heatmap -----------------------------------------------------------------
|
|
|
|
|
|
|
|
data("vaccines", package = "highcharter")
|
|
|
|
|
|
|
|
apex(vaccines, aes(year, state, fill = count), type = "heatmap") %>%
|
|
|
|
ax_chart(animations = list(enabled = FALSE)) %>%
|
|
|
|
ax_dataLabels(enabled = FALSE) %>%
|
|
|
|
ax_legend(
|
|
|
|
formatter = JS(
|
|
|
|
"function(seriesName, opts) {
|
|
|
|
if (seriesName == 'Missing') return null; else return seriesName;
|
|
|
|
}"
|
|
|
|
)
|
|
|
|
) %>%
|
|
|
|
ax_stroke(width = 0) %>%
|
|
|
|
ax_plotOptions(
|
|
|
|
heatmap = heatmap_opts(
|
|
|
|
radius = 0,
|
|
|
|
enableShades = FALSE,
|
|
|
|
colorScale = list(
|
|
|
|
ranges = list(
|
|
|
|
list(
|
|
|
|
from = 0,
|
|
|
|
to = 0.001,
|
|
|
|
name = "Missing",
|
|
|
|
color = "#FFF"
|
|
|
|
),
|
|
|
|
list(
|
|
|
|
from = 0.001,
|
|
|
|
to = 4,
|
|
|
|
name = "low",
|
|
|
|
# color = "#000004"
|
|
|
|
color = "#FDE725"
|
|
|
|
),
|
|
|
|
list(
|
|
|
|
from = 4,
|
|
|
|
to = 70,
|
|
|
|
name = "mid-low",
|
|
|
|
# color = "#781C6D",
|
|
|
|
color = "#35B779"
|
|
|
|
),
|
|
|
|
list(
|
|
|
|
from = 70,
|
|
|
|
to = 290,
|
|
|
|
name = "mid-high",
|
|
|
|
# color = "#ED6925",
|
|
|
|
color = "#31688E"
|
|
|
|
),
|
|
|
|
list(
|
|
|
|
from = 290,
|
|
|
|
to = 3000,
|
|
|
|
name = "high",
|
|
|
|
# color = "#FCFFA4",
|
|
|
|
color = "#440154"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) %>%
|
|
|
|
ax_annotations(
|
|
|
|
xaxis = list(
|
|
|
|
list(
|
|
|
|
x = 1963, x2 = 1963.1,
|
|
|
|
strokeDashArray = 0,
|
|
|
|
opacity = 1,
|
|
|
|
borderColor = "firebrick",
|
|
|
|
fillColor = "firebrick",
|
|
|
|
label = list(
|
|
|
|
borderColor = "firebrick",
|
|
|
|
|
|
|
|
style = list(color = "#FFF", background = "firebrick"),
|
|
|
|
text = "Vaccine Intoduced",
|
|
|
|
orientation = "horizontal",
|
|
|
|
position = "bottom",
|
|
|
|
offsetY = 10
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) %>%
|
|
|
|
ax_labs(
|
|
|
|
title = "Infectious Diseases and Vaccines",
|
2019-09-05 10:48:19 +02:00
|
|
|
subtitle = "vaccines dataset from {highcharter}"
|
2019-09-04 12:55:13 +02:00
|
|
|
) %>%
|
|
|
|
ax_title(
|
|
|
|
style = list(fontSize = "22px")
|
|
|
|
) %>%
|
|
|
|
ax_subtitle(
|
|
|
|
style = list(fontSize = "16px", color = "#BDBDBD")
|
|
|
|
)
|
|
|
|
|