2018-09-04 00:21:21 +02:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Title : Heatmap
|
|
|
|
# By : Victor
|
|
|
|
# Date : 2018-09-03
|
|
|
|
#
|
|
|
|
# ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Packages ----------------------------------------------------------------
|
|
|
|
|
|
|
|
library(apexcharter)
|
|
|
|
library(highcharter) # data
|
|
|
|
library(dplyr)
|
2018-09-09 22:09:05 +02:00
|
|
|
library(tidyr)
|
2018-09-04 00:21:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-09 22:09:05 +02:00
|
|
|
# Mtcars heatmap ----------------------------------------------------------
|
|
|
|
|
|
|
|
mtcars_long <- mtcars %>%
|
|
|
|
tibble::rownames_to_column(var = "model") %>%
|
|
|
|
gather(variable, value, -model)
|
|
|
|
|
2019-01-19 10:24:42 +01:00
|
|
|
mtcars_long$value <- round(mtcars_long$value)
|
2018-09-09 22:09:05 +02:00
|
|
|
|
2019-02-15 22:33:14 +01:00
|
|
|
test <- apexchart() %>%
|
2018-09-09 22:09:05 +02:00
|
|
|
ax_chart(type = "heatmap") %>%
|
|
|
|
ax_dataLabels(enabled = FALSE) %>%
|
|
|
|
ax_series2(lapply(
|
|
|
|
X = unique(mtcars_long$model),
|
|
|
|
FUN = function(x) {
|
|
|
|
list(
|
2019-01-19 10:24:42 +01:00
|
|
|
name = tolower(gsub(pattern = "\\s", replacement = "", x = x)),
|
|
|
|
data = parse_df(
|
|
|
|
data = mtcars_long[mtcars_long$model == x, c("variable", "value")],
|
|
|
|
add_names = c("x", "y")
|
|
|
|
)
|
2018-09-09 22:09:05 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)) %>%
|
|
|
|
ax_xaxis(type = "category", categories = unique(mtcars_long$variable))
|
2018-09-04 00:21:21 +02:00
|
|
|
|
|
|
|
|
2019-02-15 22:33:14 +01:00
|
|
|
apexchart() %>%
|
2019-01-19 10:24:42 +01:00
|
|
|
ax_chart(type = "heatmap") %>%
|
|
|
|
ax_dataLabels(enabled = FALSE) %>%
|
|
|
|
ax_series2(l = list(
|
|
|
|
list(
|
|
|
|
name = "A",
|
|
|
|
data = list(
|
|
|
|
list(x = "a", y = "4"), list(x = "b", y = "2"), list(x = "c", y = "7")
|
|
|
|
)
|
|
|
|
),
|
|
|
|
list(
|
|
|
|
name = "B",
|
|
|
|
data = list(
|
|
|
|
list(x = "a", y = "5"), list(x = "b", y = "4"), list(x = "c", y = "1")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
))
|
2018-09-04 00:21:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-09 22:09:05 +02:00
|
|
|
# Large Heatmap -----------------------------------------------------------
|
|
|
|
|
|
|
|
# pretty slow
|
|
|
|
|
|
|
|
# trying to recreate "The Impact of Vaccines" (http://jkunst.com/highcharter/showcase.html)
|
|
|
|
|
|
|
|
data("vaccines", package = "highcharter")
|
2018-09-04 00:21:21 +02:00
|
|
|
|
2019-02-15 22:33:14 +01:00
|
|
|
apexchart() %>%
|
2018-09-09 22:09:05 +02:00
|
|
|
ax_chart(type = "heatmap", animations = list(enabled = FALSE)) %>%
|
2018-09-04 00:21:21 +02:00
|
|
|
ax_dataLabels(enabled = FALSE) %>%
|
|
|
|
ax_series2(lapply(
|
|
|
|
X = unique(vaccines$state),
|
|
|
|
FUN = function(x) {
|
|
|
|
list(
|
|
|
|
name = x,
|
|
|
|
data = parse_df(vaccines[vaccines$state == x, c("year", "count")])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)) %>%
|
2019-09-04 12:55:13 +02:00
|
|
|
ax_legend(
|
|
|
|
formatter = JS(
|
|
|
|
"function(seriesName, opts) {
|
|
|
|
if (seriesName == 'Missing') return null; else return seriesName;
|
|
|
|
}"
|
|
|
|
)
|
|
|
|
) %>%
|
|
|
|
# ax_colors("#008FFB") %>%
|
2018-09-04 00:21:21 +02:00
|
|
|
ax_plotOptions(
|
2019-09-04 12:55:13 +02:00
|
|
|
heatmap = heatmap_opts(
|
|
|
|
radius = 0,
|
|
|
|
enableShades = FALSE,
|
2018-09-04 00:21:21 +02:00
|
|
|
colorScale = list(
|
|
|
|
ranges = list(
|
|
|
|
list(
|
|
|
|
from = 0,
|
2019-09-04 12:55:13 +02:00
|
|
|
to = 0.001,
|
|
|
|
name = "Missing",
|
|
|
|
color = "#FFF"
|
|
|
|
),
|
|
|
|
list(
|
|
|
|
from = 0.001,
|
2018-09-04 00:21:21 +02:00
|
|
|
to = 4,
|
|
|
|
name = "low",
|
2019-09-04 12:55:13 +02:00
|
|
|
# color = "#000004"
|
|
|
|
color = "#FDE725"
|
2018-09-04 00:21:21 +02:00
|
|
|
),
|
|
|
|
list(
|
|
|
|
from = 4,
|
|
|
|
to = 70,
|
|
|
|
name = "mid-low",
|
2019-09-04 12:55:13 +02:00
|
|
|
# color = "#781C6D",
|
|
|
|
color = "#35B779"
|
2018-09-04 00:21:21 +02:00
|
|
|
),
|
|
|
|
list(
|
|
|
|
from = 70,
|
|
|
|
to = 290,
|
|
|
|
name = "mid-high",
|
2019-09-04 12:55:13 +02:00
|
|
|
# color = "#ED6925",
|
|
|
|
color = "#31688E"
|
2018-09-04 00:21:21 +02:00
|
|
|
),
|
|
|
|
list(
|
|
|
|
from = 290,
|
|
|
|
to = 3000,
|
|
|
|
name = "high",
|
2019-09-04 12:55:13 +02:00
|
|
|
# color = "#FCFFA4",
|
|
|
|
color = "#440154"
|
2018-09-04 00:21:21 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2019-09-04 12:55:13 +02:00
|
|
|
) %>%
|
2018-09-04 00:21:21 +02:00
|
|
|
ax_xaxis(type = "category", categories = unique(vaccines$year))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|