[英]How to make 3 levels drilldown plot in R highcharter (possible other packages)
今天,我開始了highcharter套餐的冒險之旅。 我對下鑽圖感興趣。
(快速檢查我要在沒有r的情況下創建的內容)
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參考手冊中,只有兩個級別的示例( 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
這些深入分析的重要方面是關鍵。 向下鑽取的鍵是[名稱,值,向下鑽取]或[名稱,y,向下鑽取](因為它們主要是列向下鑽取。
df = data_frame(name = dataframe$NAMES,
y = dataframe$VALUES,
drilldown = tolower(name))
所有引用的數據應具有相同的布局(除非最后一個不會打開新的數據集)。 而且此布局應采用鍵的樣式-名稱,值和下鑽ID。 下鑽ID用作下一步鑽取的參考鍵。
有一些初始數據構成了第一組列,並具有下一組的ID。 下一組是第二層,其數據中具有第三組的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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.