R Highcharter - X 轴名称转向图表中的索引
R Highcharter - X-axis names turning to index in graph
我在 Highcharter 中有几个允许向下钻取的柱形图。我注意到,根据向下钻取的列的大小,highcharter 从显示正确的 x 轴名称切换到索引。有什么方法可以确保它始终显示 x 轴名称?
下面是一个示例代码,展示了正确显示和不正确显示的场景。唯一的变化是数据集的大小(从 400 到 50)。
library (highcharter)
rm(list=ls())
#### CASE 1 - X AXIS NAMES NOT SHOWING IN DRILLDOWN ####
h <- 0
Case1Num <- c()
for(i in 1:400){
h <- h + .02
Case1Num[i] <- h
}
Case1Name <- c()
for(i in 1:400){
Case1Name[i] <- paste0("test",i)
}
Case1Group <- c()
for(i in 1:400){
Case1Group[i] <- paste0("group1")
}
Case1 <- data.frame(Case1Name,Case1Num,Case1Group)
Lvl1 <- aggregate(Case1$Case1Num, by = list(Case1$Case1Group),FUN=sum)
Lvl1df <- data_frame(name = Lvl1$Group.1, y = Lvl1$x, drilldown = tolower((paste(name))))
dfLvl2 <- arrange(data_frame(name = Case1$Case1Name,value = Case1$Case1Num),desc(value))
hc <- highchart() %>%
hc_chart(type = "column", zoomType = "x") %>%
hc_xAxis(type = "category", labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_yAxis(gridLineWidth = 0, labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_add_series(data=Lvl1df, color = "#D20000") %>%
hc_legend(enabled = TRUE) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(id = "group1", data = list_parse2(dfLvl2))
)
)
hc
#### CASE 2 - X AXIS NAMES SHOWING CORRECTLY IN DRILLDOWN ####
h <- 0
Case2Num <- c()
for(i in 1:50){
h <- h + .02
Case2Num[i] <- h
}
Case2Name <- c()
for(i in 1:50){
Case2Name[i] <- paste0("test",i)
}
Case2Group <- c()
for(i in 1:50){
Case2Group[i] <- paste0("group1")
}
Case2 <- data.frame(Case2Name,Case2Num,Case2Group)
Lvl1 <- aggregate(Case2$Case2Num, by = list(Case2$Case2Group),FUN=sum)
Lvl1df <- data_frame(name = Lvl1$Group.1, y = Lvl1$x, drilldown = tolower((paste(name))))
dfLvl2 <- arrange(data_frame(name = Case2$Case2Name,value = Case2$Case2Num),desc(value))
hc2 <- highchart() %>%
hc_chart(type = "column", zoomType = "x") %>%
hc_xAxis(type = "category", labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_yAxis(gridLineWidth = 0, labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_add_series(data=Lvl1df, color = "#D20000") %>%
hc_legend(enabled = TRUE) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(id = "group1", data = list_parse2(dfLvl2))
)
)
hc2
这是 Highcharts 的未决问题。不仅在 R 中,而且在 javascript 中也是如此。
此问题已在此处报告:
看来问题已在 github 讨论帖中解决。
将cropThreshold = 1000
添加到hc_plotOptions
就解决了这个问题。它通常默认设置为 50
,这就是为什么上面的内容被视为更大的原因。
hc2 <- highchart() %>%
hc_chart(type = "column", zoomType = "x") %>%
hc_xAxis(type = "category", labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_yAxis(gridLineWidth = 0, labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_add_series(data=Lvl1df, color = "#D20000") %>%
hc_legend(enabled = TRUE) %>%
hc_plotOptions(column = list(
cropThreshold = 1000,
stacking = "normal")) %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(id = "group1", data = list_parse2(dfLvl2))
)
)
我在 Highcharter 中有几个允许向下钻取的柱形图。我注意到,根据向下钻取的列的大小,highcharter 从显示正确的 x 轴名称切换到索引。有什么方法可以确保它始终显示 x 轴名称?
下面是一个示例代码,展示了正确显示和不正确显示的场景。唯一的变化是数据集的大小(从 400 到 50)。
library (highcharter)
rm(list=ls())
#### CASE 1 - X AXIS NAMES NOT SHOWING IN DRILLDOWN ####
h <- 0
Case1Num <- c()
for(i in 1:400){
h <- h + .02
Case1Num[i] <- h
}
Case1Name <- c()
for(i in 1:400){
Case1Name[i] <- paste0("test",i)
}
Case1Group <- c()
for(i in 1:400){
Case1Group[i] <- paste0("group1")
}
Case1 <- data.frame(Case1Name,Case1Num,Case1Group)
Lvl1 <- aggregate(Case1$Case1Num, by = list(Case1$Case1Group),FUN=sum)
Lvl1df <- data_frame(name = Lvl1$Group.1, y = Lvl1$x, drilldown = tolower((paste(name))))
dfLvl2 <- arrange(data_frame(name = Case1$Case1Name,value = Case1$Case1Num),desc(value))
hc <- highchart() %>%
hc_chart(type = "column", zoomType = "x") %>%
hc_xAxis(type = "category", labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_yAxis(gridLineWidth = 0, labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_add_series(data=Lvl1df, color = "#D20000") %>%
hc_legend(enabled = TRUE) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(id = "group1", data = list_parse2(dfLvl2))
)
)
hc
#### CASE 2 - X AXIS NAMES SHOWING CORRECTLY IN DRILLDOWN ####
h <- 0
Case2Num <- c()
for(i in 1:50){
h <- h + .02
Case2Num[i] <- h
}
Case2Name <- c()
for(i in 1:50){
Case2Name[i] <- paste0("test",i)
}
Case2Group <- c()
for(i in 1:50){
Case2Group[i] <- paste0("group1")
}
Case2 <- data.frame(Case2Name,Case2Num,Case2Group)
Lvl1 <- aggregate(Case2$Case2Num, by = list(Case2$Case2Group),FUN=sum)
Lvl1df <- data_frame(name = Lvl1$Group.1, y = Lvl1$x, drilldown = tolower((paste(name))))
dfLvl2 <- arrange(data_frame(name = Case2$Case2Name,value = Case2$Case2Num),desc(value))
hc2 <- highchart() %>%
hc_chart(type = "column", zoomType = "x") %>%
hc_xAxis(type = "category", labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_yAxis(gridLineWidth = 0, labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_add_series(data=Lvl1df, color = "#D20000") %>%
hc_legend(enabled = TRUE) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(id = "group1", data = list_parse2(dfLvl2))
)
)
hc2
这是 Highcharts 的未决问题。不仅在 R 中,而且在 javascript 中也是如此。 此问题已在此处报告:
看来问题已在 github 讨论帖中解决。
将cropThreshold = 1000
添加到hc_plotOptions
就解决了这个问题。它通常默认设置为 50
,这就是为什么上面的内容被视为更大的原因。
hc2 <- highchart() %>%
hc_chart(type = "column", zoomType = "x") %>%
hc_xAxis(type = "category", labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_yAxis(gridLineWidth = 0, labels = list(style = list(fontSize = "1.1vh"))) %>%
hc_add_series(data=Lvl1df, color = "#D20000") %>%
hc_legend(enabled = TRUE) %>%
hc_plotOptions(column = list(
cropThreshold = 1000,
stacking = "normal")) %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(id = "group1", data = list_parse2(dfLvl2))
)
)