如何在R highcharter中制作3个级别的深入绘图(可能的其他软件包) [英] How to make 3 levels drilldown plot in R highcharter (possible other packages)

查看:133
本文介绍了如何在R highcharter中制作3个级别的深入绘图(可能的其他软件包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我开始了我的冒险与highcharter包。我对钻取地块感兴趣。



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

< href =http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/drilldown/basic/ =nofollow> 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(名称)

df


ds < - list.parse3(df)
names(ds)< - NULL
str(ds)
hc< - highchart()%>%
hc_chart( (%)
hc_legend(已启用)%>%
hc_title(text =Basic drilldown)%>%
hc_xAxis(type =category)%>% = 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)
)$ b $ (b)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级的向下钻取图。

  library(dplyr)这是可能的(在javascrip Highchart页面上有3级示例,但用js编写) )
library(purrr)
library(highcharter)


df< - data_frame(
name = c(Animals ,水果,汽车),
y = c(5,2,4),
钻取= tolower(名称)

df


ds< - list.parse3(df)
名称(ds)< - NULL
str(ds)


hc< ; - 高图(%)>%
hc_chart(type =column)%>%
hc_title(text =Basic drilldown)%>%
hc_xAxis(type = (
boderWidth = 0,
dataLabels =

hc_legend(enabled = FALSE)%>%
hc_plotOptions列表(启用=真)

)%>%
hc_add_series(
name =Things,
colorByPoint = TRUE,
data = ds



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


dffru< - data_frame(
name = c(Apple,Oranges)) ,
值= 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
})
$ b $ 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,
数据= dsan
),
列表(
id =fruits,
data = dsfru
),
列表(
id = 汽车,
data = dscar

),
#我有变化的想法。
list2 = 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设置为数据点,就像在pure js highcharts中一样。


$ b 示例: a href =http://jsfiddle.net/6LXVQ/2/ =nofollow> http://jsfiddle.net/6LXVQ/2/
和最重要的部分:


 钻取:{
series:[{
id:'animals',
name: '动物',
数据:[{
name:'Cats ,
y:4,
钻取:'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 =动物


)%& gt;%

hc_drilldown(
series = list(
list(
name =Animals,
id =animals,
数据=清单(
清单(
名称=猫,
y = 2,
钻取=猫


),
list(
name =Cats,
id =cats,
data = list(list(name =white cats,y = 2) ,列表(名称=黑猫,y = 3),列表(名称=红猫,y = 4))



hc


today i started my adventure with highcharter package. I'm interested in drilldown plots.

(fast check what i want to create without r)

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

R code with working example of drilldown plot with 2 levels.

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

My aim is to create drilldown plots with more than 2 levels. I know this is possible (On javascrip Highchart page there is 3 level example but written in 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

In highcharter reference manual there is only example with 2 levels (https://cran.r-project.org/web/packages/highcharter/highcharter.pdf)

解决方案

If you want a multilevel drilldown you have to set id of the drilldown to the data point just like in the pure js highcharts.

Example: http://jsfiddle.net/6LXVQ/2/ and the most important part:

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]
        }]
    }

You can see here that your data points are not only numbers but objects which containts link to the drilldown series.

An example using Highcharter - simplified but you should get the idea:

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

这篇关于如何在R highcharter中制作3个级别的深入绘图(可能的其他软件包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆