R海查特马赛克/马里梅科图

dced5bon  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(166)

我试着在R Highcharter中创建一个marimekko图表,如下所示:
http://jsfiddle.net/highcharts/h2np93k1/
我似乎无法使树图的sortIndex工作,我的代码如下:

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))))

有人有什么想法吗?

py49o6xq

py49o6xq1#

我意识到这个问题是几年前的事了,自从它被写出来以来,海查特已经改变了。
这个答案在2018年可能行不通,但现在确实行得通。
hctreemaphctreemap2在某些时候被弃用了。Highcharter的指令是使用data_to_hierarchical准备数据,然后使用hchart()highchart()创建树图。然而,这个方法将剥离sortIndex,所以我不认为这是您想要走的路线。
相反,我准备的数据格式,因为它是在JS链接提供,然后图形化。
数据:

# 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
              }))

现在,数据已准备好进行绘图。

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

相关问题