今天,我开始了高包套餐之旅。我对下钻图感兴趣。

(快速检查我要在没有r的情况下创建的内容)

http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/drilldown/basic/

R代码,带2个层的向下钻取图工作示例。

library("dplyr")
library("purrr")
library("highcharter")

df <- data_frame(
  name = c("Animals", "Fruits", "Cars"),
  y = c(5, 2, 4),
  drilldown = tolower(name)
)
df


ds <- list.parse3(df)
names(ds) <- NULL
str(ds)
hc <- highchart() %>%
  hc_chart(type = "column") %>%
  hc_title(text = "Basic drilldown") %>%
  hc_xAxis(type = "category") %>%
  hc_legend(enabled = FALSE) %>%
  hc_plotOptions(
    series = list(
      boderWidth = 0,
      dataLabels = list(enabled = TRUE)
    )
  ) %>%
  hc_add_series(
    name = "Things",
    colorByPoint = TRUE,
    data = ds
  )


dfan <- data_frame(
  name = c("Cats", "Dogs", "Cows", "Sheep", "Pigs"),
  value = c(4, 3, 1, 2, 1)
)
dffru <- data_frame(
  name = c("Apple", "Organes"),
  value = c(4, 2)
)
dfcar <- data_frame(
  name = c("Toyota", "Opel", "Volkswage"),
  value = c(4, 2, 2)
)
second_el_to_numeric <- function(ls){
  map(ls, function(x){
    x[[2]] <- as.numeric(x[[2]])
    x
  })
}
dsan <- second_el_to_numeric(list.parse2(dfan))
dsfru <- second_el_to_numeric(list.parse2(dffru))
dscar <- second_el_to_numeric(list.parse2(dfcar))
hc <- hc %>%
  hc_drilldown(
    allowPointDrilldown = TRUE,
    series = list(
      list(
        id = "animals",
        data = dsan
      ),
      list(
        id = "fruits",
        data = dsfru
      ),
      list(
        id = "cars",

        data = dscar
      )
    )
  )
hc


我的目标是创建具有2个以上级别的明细图。我知道这是可能的(在javascrip Highchart页面上有3级示例,但是用js编写)。

library("dplyr")
library("purrr")
library("highcharter")


df <- data_frame(
  name = c("Animals", "Fruits", "Cars"),
  y = c(5, 2, 4),
  drilldown = tolower(name)
)
df


ds <- list.parse3(df)
names(ds) <- NULL
str(ds)


hc <- highchart() %>%
  hc_chart(type = "column") %>%
  hc_title(text = "Basic drilldown") %>%
  hc_xAxis(type = "category") %>%
  hc_legend(enabled = FALSE) %>%
  hc_plotOptions(
    series = list(
      boderWidth = 0,
      dataLabels = list(enabled = TRUE)
      )
  ) %>%
  hc_add_series(
    name = "Things",
    colorByPoint = TRUE,
    data = ds
  )


dfan <- data_frame(
  name = c("Cats", "Dogs", "Cows", "Sheep", "Pigs"),
  value = c(4, 3, 1, 2, 1)

)
dffru <- data_frame(
  name = c("Apple", "Oranges"),
  value = c(4, 2)
)
dfcar <- data_frame(
  name = c("Toyota", "Opel", "Volkswage"),
  value = c(4, 2, 2),
  drilldown = tolower(name)
)


dfOpel <- data_frame(
  name = c("Insygnia", "Corsa"),
  value = c(1,2)
)


second_el_to_numeric <- function(ls){
  map(ls, function(x){
    x[[2]] <- as.numeric(x[[2]])
    x
  })
}
dsan <- second_el_to_numeric(list.parse2(dfan))
dsfru <- second_el_to_numeric(list.parse2(dffru))
dscar <- second_el_to_numeric(list.parse3(dfcar))
names(dscar) <- NULL

dsOpel <- second_el_to_numeric(list.parse3(dfOpel))
names(dsOpel)

hc <- hc %>%
  hc_drilldown(
    allowPointDrilldown = TRUE,
    series = list(
      list(
        id = "animals",
        data = dsan
      ),
      list(
        id = "fruits",
        data = dsfru
      ),
      list(
        id = "cars",
        data = dscar
      )
    ),
#My idea of change.
    series2 = list(
      list(id = "toyota", data = dsOpel),
      list(id = "opel", data = dsOpel),
      list(id = "volkswage", data = dsOpel)
    )
  )


hc


在highcharter参考手册中,仅包含2个级别(https://cran.r-project.org/web/packages/highcharter/highcharter.pdf)的示例

最佳答案

如果要进行多级钻取,则必须将钻取的id设置为数据点,就像在纯js highcharts中一样。

示例:http://jsfiddle.net/6LXVQ/2/
最重要的部分:

drilldown: {
        series: [{
            id: 'animals',
            name: 'Animals',
            data: [{
                name: 'Cats',
                y: 4,
                drilldown: 'cats'
            }, ['Dogs', 2],
                ['Cows', 1],
                ['Sheep', 2],
                ['Pigs', 1]
            ]
        }, {

            id: 'cats',
            data: [1, 2, 3]
        }]
    }


您可以在此处看到您的数据点不仅是数字,而且还包含链接到明细系列的对象。

使用Highcharter的示例-简化了,但您应该了解一下:

hc <- highchart() %>%
    hc_chart(type="column") %>%
    hc_xAxis(type="category") %>%
    hc_add_series(
        name = "Things",
        data = list(
            list(
                name = "Animals",
                y = 10,
                drilldown = "animals"
            )
        )
    ) %>%

    hc_drilldown(
        series = list(
            list(
                name = "Animals",
                id = "animals",
                data = list(
                    list(
                        name = "Cats",
                        y = 2,
                        drilldown = "cats"
                    )
                )
             ),
             list(
                 name = "Cats",
                 id = "cats",
                 data = list(list(name = "white cats", y = 2), list(name = "black cats", y = 3), list(name = "red cats",y = 4))
             )
         )
     )
hc

08-25 07:06