【问题标题】:How to make 3 levels drilldown plot in R highcharter (possible other packages)如何在 R highcharter 中制作 3 个级别的向下钻取图(可能的其他包)
【发布时间】:2019-02-14 18:33:10
【问题描述】:

今天我从 highcharter 包开始了我的冒险。我对向下钻取图感兴趣。

(快速检查我想在没有 r 的情况下创建什么)

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

带有 2 个级别的钻取图的工作示例的 R 代码。

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)

【问题讨论】:

    标签: javascript r highcharts drilldown


    【解决方案1】:

    如果你想要一个多级向下钻取,你必须将向下钻取的 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
    

    【讨论】:

    • 在您的示例小提琴中,当您一直向下钻到 Cats 时,使用“回到动物”进行向上钻取,然后再次向下钻到 Cats ...您最终会在每个序列的开头。这是一个错误吗?
    【解决方案2】:

    这些钻取的重要方面是关键。向下钻取的键是 [name, value, Drilldown] 或 [name, y, Drilldown](因为它们主要是列向下钻取。

    df = data_frame(name = dataframe$NAMES,
                   y = dataframe$VALUES,
                   drilldown = tolower(name))
    

    所有引用的数据都应该具有相同的布局(除非最后一个没有打开到一组新数据中)。这种布局应该是键的模式——名称、值和向下钻取 ID。向下钻取 ID 用作下一步向下钻取的参考键。

    初始数据构成第一组列,并具有下一组的 ID。下一组是第二层,其数据中包含第三组的 ID。第三组构成第三层。

    示例:在宠物、鸟类和两栖动物的数据集中:Pets 下一层是 Cats、Dogs、Hamsters、Fish。 Pets 中的每个名字都附有一个 ID。猫会通过该 ID 拉入虎斑、棕色、黑色和汤姆。狗会从其 ID 中拉出斗牛犬、哈巴狗、实验室柯基犬,仓鼠也是如此。

    #LAYER ONE OF DRILLDOWN
    animalsdf = data_frame(name = animals$NAMES,
                   y = animals$VALUES,
                   drilldown = tolower(paste(name,'id')))
    
    #Example of drilldown ID's here: 'pets id', 'birds id', 'amphibians id'
    
    animalsds = list_parse(animalsdf)
    
    names(animalsds) = NULL
    
    #LAYER TWO OF DRILLDOWN
    petsdf = data_frame(name = typeofpets$NAMES,
                   y = typeofpets$VALUES,
                   drilldown = tolower(paste(name,'id')))
    
    birdsdf = data_frame(name = typeofbirds$NAMES,
                   y = typeofbirds$VALUES,
                   drilldown = tolower(paste(name,'id')))
    
    amphibiansdf = data_frame(name = typeofamphibians$NAMES,
                   y = typeofamphibians$VALUES,
                   drilldown = tolower(paste(name,'id')))
    
    petsds <- second_el_to_numeric(list_parse2(petsdf))
    birdsds <- second_el_to_numeric(list_parse2(birdsdf))
    amphibiansds <- second_el_to_numeric(list_parse2(amphibiansdf))
    
    #LAYER THREE OF DRILLDOWN
    
    #FOR PETS
    
    catsdf = data_frame(name = typeofcats$NAMES,
                   y = typeofcats$VALUES,
                   drilldown = tolower(paste(name,'id')))
    
    dogsdf= data_frame(name = typeofdogs$NAMES,
                   y = typeofdogs$VALUES,
                   drilldown = tolower(paste(name,'id')))
    
    hamstersdf = data_frame(name = typeofhamsters$NAMES,
                   y = typeofhamsters$VALUES,
                   drilldown = tolower(paste(name,'id')))
    
    
    catsds <- second_el_to_numeric(list_parse2(catsdf))
    dogsds <- second_el_to_numeric(list_parse2(dogsdf))
    hamstersds <- second_el_to_numeric(list_parse2(hamstersdf))
    
    #FOR BIRDS
    flightlessbirdsdf = data_frame(name = flightlessbirds$NAMES,
                   y = flightlessbirds$VALUES,
                   drilldown = tolower(paste(name,'id')))
    
    flyingbirdsdf = data_frame(name = flyingbirds$NAMES,
                   y = flyingbirds$VALUES,
                   drilldown = tolower(paste(name,'id')))
    
    flightlessbirdsds <- second_el_to_numeric(list_parse2(flightlessbirdsdf))
    flyingbirdsds <- second_el_to_numeric(list_parse2(flyingbirdsdf))
    
    #FOR AMPHIBIANS
    
    largeamphibiansdf = data_frame(name = largeamphibians$NAMES,
                   y = flyingbirds$VALUES,
                   drilldown = tolower(paste(name,'id')))
    
    smallamphibiansdf = data_frame(name = smallamphibians$NAMES,
                   y = smallamphibians$VALUES,
                   drilldown = tolower(paste(name,'id')))
    
    largeamphibiansds <- second_el_to_numeric(list_parse2(largeamphibiansdf))
    smallamphibiansds <- second_el_to_numeric(list_parse2(smallamphibiansdf))
    
    #HIGHCHART STARTS
    
    hc <- highchart() %>%
    hc_chart(type = "column") %>% 
    hc_title(text = "Drilldown") %>%
    hc_subtitle(text = "XYZ") %>%
    hc_xAxis(type = "category") %>%
    hc_legend(enabled = FALSE) %>%
    hc_plotOptions(
      series = list(
        boderWidth = 0,
        dataLabels = list(enabled = TRUE)
      )
    ) %>%
    hc_add_series(
      name = "Category",
      colorByPoint = TRUE,
      data = animalsds
    )  %>%
    hc_drilldown(
      allowPointDrilldown = TRUE,
      series = list(
        list(
          id = "pets id",
          data = petsds,
          keys = list('name','y','drilldown')
        ),
        list(
          id = "birds id",
          data = birdsds,
          keys = list('name','y','drilldown')
        ),
        list(
          id = "amphibians id",
          data = amphibiansds,
          keys = list('name','y','drilldown')
        ),
        list(
          id = "cats id",
          data = catsds,
        ),
        list(
          id = "dogs id",
          data = dogsds
        ),
        list(
          id = "hamsters id",
          data = hamstersds
        ),
        list(
          id = "flightless birds id",
          data = flightlessbirdsds
        ),
        list(
          id = "flying birds id",
          data = flyingbirdsid
        ),
        list(
          id = "large amphibians id",
          data = largeamphibiansds
        ),
        list(
          id = "small amphibians id",
          data = smallamphibiansds
        )
    )) %>% hc_tooltip(valueDecimals = 2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-18
      • 2018-02-17
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多