R Highcharter mosaïc / marimekko chart

145 Views Asked by At

i tried to create a marimekko chart in R Highcharter, following this example :

http://jsfiddle.net/highcharts/h2np93k1/

I cannot seem to get the sortIndex of the treemap to work, my code is as follows:

parentid <- c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
sortIndex <- c(0, 1, 2, 3, 4, 0, 1, 2, 3, 4)
child <- c("Alpha", "Alpha", "Alpha", "Alpha", "Alpha", "Beta", "Beta", "Beta", "Beta", "Beta")
childid <- c(100, 100, 100, 100, 100, 200, 200, 200, 200, 200)
colorid <- c(100, 100, 100, 100, 100, 200, 200, 200, 200, 200)
parent <- c("Parent 1", "Parent 2", "Parent 3", "Parent 4", "Parent 5", "Parent 1", "Parent 2", "Parent 3", "Parent 4", "Parent 5")
value <- c(10, 60, 70, 20, 90, 50, 30, 10, 90, 10)
data <- data.frame(parentid, sortIndex, child, childid, colorid, parent, value)

hctreemap2(data, group_vars=c("parentid", "childid"),
           size_var="value",
           color_var="colorid",
           layoutAlgorithm='stripes',
           alternateStartingDirection = T,
           stacking="percent",
           levelIsConstant = F,
           sortIndex=sortIndex,
           levels = list(
             list(level=1, dataLabels = list(enabled=T, align='left', verticalAlign='top'), borderWidth=3),
             list(level=2, dataLabels = list(enabled=T))))

does anyone have any ideas?

1

There are 1 best solutions below

0
On

I realize this question was several years ago, and Highcharter has changed since it was written.

This answer may not have worked in 2018, but it does work now.

At some point hctreemap and hctreemap2 were deprecated. The instructions from Highcharter are to use data_to_hierarchical to prepare the data and then use either hchart() or highchart() to create the treemap. However, this method will strip the sortIndex, so I don't think that's the route you would want to go.

Instead, I prepared the data by formatting it as it is in the JS link you provided and then graphed it.

The data:

# collect hc colors (like they did in the example)
colrs <- getOption("highcharter.color_palette")

# two data frames, one for each level
# id isn't as important until you go beyond 2 levels
pars <- data.frame(id = unique(data$parent),sortIndex = unique(data$sortIndex))
kids <- data.frame(
  name = data$child, parent = data$parent, sortIndex = data$sortIndex,
  value = data$value, color = data$color/100) %>% mutate(color = colrs[color])

# this assumes data is already sorted by sort order*
newData <- list()  # for storing the data as hierarchical
invisible(map(1:nrow(pars),
              function(j) {
                p <- pars[j, ]$id           # collect par id to find children
                k <- kids[kids$parent == p, ] # isolate applicable children
                pl <- list_parse(pars[j, ]) # make row a list
                kl <- list_parse(k)         # make each child row a list
                newData <<- append(newData, pl) # add parent
                newData <<- append(newData, kl) # add that parent's children
              }))

Now the data is ready for plotting.

hchart(newData, type ="treemap", layoutAlgorithm = "stripes",
       alternateStartingDirection = T)

enter image description here