knitr::opts_chunk$set(echo = TRUE) library(tidyverse) library(highcharter) library(treemap) library(RColorBrewer) data(GNI2014)
This pull request is to address the following issues in the Highcharter function hctreemap
:
dplyr
>= 0.5.0.9005rlang
>= 0.1.1Both of these are included in the latest release of the tidyverse
to CRAN. I got a R CMD Check Note for using NSE, which I resolved by adding
if (getRversion() >= "2.15.1") utils::globalVariables(c(".", "colorValue", "level", "name", "parent"))
to the highcharter-package.R
file. This is new to me, so I figured if you have a preference for how to sort this out you'll know how to change this better than I do.
This pull request includes the function hctreemap2
which has the following interface
hctreemap2(data, group_vars, size_var, color_var = NULL, ...)
Note that this function accepts a dataframe rather than a treemap::treemap
object. hctreemap
remains unaltered, albeit with a deprecation warning.
The old interface looks like this
treemap(GNI2014, index = c("continent", "iso3"), vSize = "population", vColor = "GNI", type = "value", draw = FALSE) %>% hctreemap(allowDrillToNode = TRUE, layoutAlgorithm = "squarified") %>% hc_tooltip(pointFormat = "<b>{point.name}</b>:<br> Pop: {point.value:,.0f}<br> GNI: {point.valuecolor:,.0f}")
and the new interface looks very similar
hctreemap2(data = GNI2014, group_vars = c("continent", "iso3"), size_var = "population", color_var = "GNI", layoutAlgorithm = "squarified", levelIsConstant = FALSE, levels = list( list(level = 1, dataLabels = list(enabled = TRUE)), list(level = 2, dataLabels = list(enabled = FALSE)), list(level = 3, dataLabels = list(enabled = FALSE)) )) %>% hc_colorAxis(minColor = brewer.pal(7, "Greens")[1], maxColor = brewer.pal(7, "Greens")[7]) %>% hc_tooltip(pointFormat = "<b>{point.name}</b>:<br> Pop: {point.value:,.0f}<br> GNI: {point.colorValue:,.0f}")
Likely the biggest difference for users will be that they'll need to specify the color palette themselves via hc_colorAxis
rather than being able to make use of the palette
argument in treemap::treemap
. By default, hctreemap2
enables a color axis with minimum and maximum values determined by at the lowest depth in the treemap, in keeping with treemap::treemap
. I've included the above as an example in the function documentation, so hopefully people will get the hint about how to use RColorBrewer
to make their lives better.
If the user doesn't specify a variable for the color mapping, the variable for the size mapping is used by default. Additionally, allowDrillToNode
defaults to TRUE
.
We can make a rather boring three level treemap to demonstrate that three level treemaps work
data.frame(index1 = sample(LETTERS[1:3], 500, replace = T), index2 = sample(LETTERS[4:6], 500, replace = T), index3 = sample(LETTERS[7:9], 500, replace = T), value = rpois(500, 5)) %>% hctreemap2( group_vars = c("index1", "index2", "index3"), size_var = "value" )
set.seed(27) data.frame( index1 = sample(LETTERS[1:6], 500, replace = T), index2 = sample(LETTERS[1:6], 500, replace = T), value = abs(rnorm(500))) %>% hctreemap2(c("index1", "index2"), "value")
The error checking also catches missing data:
data.frame( index1 = c(1, NA), index2 = c(NA, 2), value = abs(rnorm(500))) %>% hctreemap2(c("index1", "index2"), "value")
This forces users to recode their data before continuing and should allow arbitrary depth treemaps. Lastly, a single level treemap just as another example
hctreemap2(data = GNI2014, group_vars = "continent", size_var = "population") %>% hc_colorAxis(minColor = brewer.pal(7, "Greens")[1], maxColor = brewer.pal(7, "Greens")[7]) %>% hc_tooltip(pointFormat = "<b>{point.name}</b>:<br> Pop: {point.value:,.0f}<br> GNI: {point.colorValue:,.0f}")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.