Skip to content

Commit

Permalink
working toward PR for hctreemap2
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpghayes committed Jun 19, 2017
1 parent 39c7f8b commit affc48e
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
82 changes: 82 additions & 0 deletions R/hchart-shorcuts.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ hciconarray <- function(labels, counts, rows = NULL, icons = NULL, size = 4,
#' @export
hctreemap <- function(tm, ...) {

.Deprecated("hctreemap2")

assertthat::assert_that(is.list(tm))

df <- tm$tm %>%
Expand Down Expand Up @@ -366,6 +368,86 @@ hctreemap <- function(tm, ...) {

}

#' Shortcut to create treemaps.
#'
#' This function helps create highcharts treemaps from data frames.
#'
#' @param data data frame containing variables to organize each level of the treemap on
#' @param group_vars vector of strings containing column names of variables to generate treemap levels from. the first listed column will specify the top level of the treemap. the unique values in each of these columns must have no intersection (including NAs).
#' @param size_var string name of column containing numeric data to aggregate by
#' @param color_var string name of column containing numeric data to color by. defaults to same column as \code{size_var}
#' @param ... ... additional shared arguments for the data series
#' (\url{http://api.highcharts.com/highcharts#series}).
#'
#' @return highchart plot object
#' @export
#'
#' @examples
#'
#' \dontrun{
#'
#' data <- data.frame(
#' index1 = sample(LETTERS[10:15], 100, replace = T),
#' index2 = sample(LETTERS[16:21], 100, replace = T),
#' index3 = sample(LETTERS[22:26], 100, replace = T),
#' x = abs(rnorm(100)),
#' y = rpois(100, lambda = 3)
#' )
#'
#' hctreemap3(data,
#' c("index1", "index2", "index3"),
#' "x",
#' "y",
#' 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))
#' ))
#' }
#'
hctreemap2 <- function(data, group_vars, size_var, color_var = NULL, ...) {

group_syms <- rlang::syms(group_vars)
size_sym <- rlang::sym(size_var)
color_sym <- rlang::sym(ifelse(is.null(color_var), size_var, color_var))

if (data %>%
select(!!!group_syms) %>%
map(unique) %>%
unlist() %>%
anyDuplicated()) stop("Treemap data uses same category name at multiple levels.")

data_at_depth <- function(depth) {
data %>%
group_by(!!!group_syms[1:depth]) %>%
summarise(size = sum(!!size_sym), colorValue = sum(!!color_sym)) %>%
ungroup() %>%
mutate(value = size, name = !!group_syms[[depth]], level = depth) %>%
{
if (depth == 1) mutate(., parent = NA, id = paste0(name, 1))
else {
mutate(.,
parent = paste(!!!group_syms[1:depth-1], 1:(depth-1), sep = "_"),
id = paste(parent, paste0(name, depth), sep = "_")
)
}
} %>%
mutate_at(vars(name, parent, id), as.character)
}

hd <- 1:length(group_vars) %>%
map(data_at_depth) %>%
bind_rows() %>%
highcharter::list_parse() %>%
purrr::map(~.[!is.na(.)])

highchart() %>%
hc_add_series(data = hd, type = "treemap", allowDrillToNode = TRUE, ...) %>%
hc_colorAxis(enabled = TRUE)
}

#' Shortcut for create parallel coordinates
#' @param df A data frame object.
#' @param ... Additional shared arguments for the data series
Expand Down
86 changes: 86 additions & 0 deletions dev/treemap2.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
title: "Demostration of New Function `hctreemap2`"
author: "Alex Hayes"
date: "June 18, 2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(highcharter)
library(highcharter)
library(data.tree)
library(treemap)
data(GNI2014, "treemap")
rm(list = ls())
options(highcharter.debug = TRUE)
```

This pull request is to address the following issues in Highcharter:

* hctreemap() fails to render with cellnames repeated across levels #310
* bug in treemap when index variables have same values #32
* Does not work correctly for 3 level Treemaps #117
* hc_add_series_treemap - naming of the color value variable #98
* review/rework treemap fns #110

# Interface Change and Old Interface Deprecation

# Example Use

# hctreemap2 tests

# single level treemap

# two level treemap

# three level treemap

# throws non-unique labelling error

dup_data <- data.frame(
index1 = sample(LETTERS[1:6], 500, replace = T),
index2 = sample(LETTERS[1:6], 500, replace = T),
index3 = sample(LETTERS[1:6], 500, replace = T),
x = abs(rnorm(500))
)

dup_data <- data.frame(
index1 = c(1, NA), #sample(NA, 500, replace = T),
index2 = c(NA, 2), #sample(NA, 500, replace = T),
x = abs(rnorm(500))
)

hctreemap2(dup_data, c("index1", "index2", "index3"), "x")

# color schemes: user must specify the color gradient to hc_colorAxis

# speed test compared to proposed development version

# current development version seems to have an error using color var


treemap(GNI2014, index = c("continent", "iso3"),
vSize = "population", vColor = "GNI",
type = "comp",
draw = FALSE) %>%
hctreemap(allowDrillToNode = TRUE, layoutAlgorithm = "squarified") %>%
hc_title(text = "Gross National Income World Data") %>%
hc_tooltip(pointFormat = "<b>{point.name}</b>:<br>
Pop: {point.value:,.0f}<br>
GNI: {point.valuecolor:,.0f}")

# issue: does not work with treemap objects created by treemap package

hc_colorAxis(minColor = tm$tm$color[which.min(tm$tm$vColorValue)],
maxColor = tm$tm$color[which.max(tm$tm$vColorValue)])

# speed tests
big_tree <- data.frame(
index1 = sample(LETTERS[10:16], 500, replace = T),
index2 = sample(LETTERS[16:22], 500, replace = T),
index3 = sample(LETTERS[22:26], 500, replace = T),
x = abs(rnorm(500)),
y = rpois(500, lambda = 3)
)

0 comments on commit affc48e

Please sign in to comment.