Highcharter - 添加多个文本注释
Highcharter - add multiple text annotations
我有一个堆积柱形图,我需要在每列下方添加一个文本注释。我只找到了一种添加一个注释的方法。添加第二个注释会产生错误 All arguments must be named list
。遗憾的是,我知道如何使用 hc_annotations 函数。
这是我尝试过的一个小示例和代码位:
hc <- highchart() %>%
hc_chart(type = "column") %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_title(text = "Plot with Annotations", useHTML = TRUE) %>%
hc_yAxis(title = "") %>%
hc_xAxis(title = "") %>%
hc_xAxis(categories = c("A", "B", "C", "D", "E"))
hc <- hc %>%
hc_add_series(name = "S1", data = c(5, 1, 2, 4, 5),
dataLabels = list(format='{point.y:,.1f}', align = "center", enabled = TRUE))
hc <- hc %>%
hc_add_series(name = "S2", data = c(-1, -4, 3, -2, -4),
dataLabels = list(format='{point.y:,.1f}', align = "center", enabled = TRUE))
##try to add annotations
#1
hc <- hc %>%
hc_annotations(list(xValue = 0, yValue = -2, title = list(text = '-6 pp')))
hc <- hc %>%
hc_annotations(list(xValue = 1, yValue = -8, title = list(text = '-5 pp')))
#2 - basically the same as #1
hc <- hc %>%
hc_annotations(list(xValue = 0, yValue = -2, title = list(text = '-6 pp'))) %>%
hc_annotations(list(xValue = 1, yValue = -8, title = list(text = '-5 pp')))
#3
hc <- hc %>%
hc_annotations(list(list(xValue = 0, yValue = -2, title = list(text = '-6 pp')),
list(xValue = 0, yValue = -8, title = list(text = '-5 pp')))
)
hc
我只是在这里使用了两个注释作为展示。在我的最终代码中,每一列下面都需要一个注释。
这是一个已知问题,已通过下一个函数解决:
hc_add_annotation
hc_add_annotations
使用示例:
hc %>%
hc_add_annotation(xValue = 0, yValue = -2, title = list(text = '-6 pp')) %>%
hc_add_annotation(xValue = 1, yValue = -4.5, title = list(text = '-5 pp'))
hc %>%
hc_add_annotations(
list(
list(xValue = 0, yValue = -2, title = list(text = '-6 pp')),
list(xValue = 1, yValue = -4.5, title = list(text = '-5 pp'))
)
)
甚至更好:
df <- data_frame(
xValue = c(0, 1),
yValue = c(-2, -4.5),
title = c("-6pp", "-5pp")
)
df <- df %>%
mutate(title = map(title, function(x) list(text = x)))
df
#> # A tibble: 2 x 3
#> xValue yValue title
#> <dbl> <dbl> <list>
#> 1 0 -2.0 <list [1]>
#> 2 1 -4.5 <list [1]>
hc %>%
hc_add_annotations(df)
我有一个堆积柱形图,我需要在每列下方添加一个文本注释。我只找到了一种添加一个注释的方法。添加第二个注释会产生错误 All arguments must be named list
。遗憾的是,我知道如何使用 hc_annotations 函数。
这是我尝试过的一个小示例和代码位:
hc <- highchart() %>%
hc_chart(type = "column") %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_title(text = "Plot with Annotations", useHTML = TRUE) %>%
hc_yAxis(title = "") %>%
hc_xAxis(title = "") %>%
hc_xAxis(categories = c("A", "B", "C", "D", "E"))
hc <- hc %>%
hc_add_series(name = "S1", data = c(5, 1, 2, 4, 5),
dataLabels = list(format='{point.y:,.1f}', align = "center", enabled = TRUE))
hc <- hc %>%
hc_add_series(name = "S2", data = c(-1, -4, 3, -2, -4),
dataLabels = list(format='{point.y:,.1f}', align = "center", enabled = TRUE))
##try to add annotations
#1
hc <- hc %>%
hc_annotations(list(xValue = 0, yValue = -2, title = list(text = '-6 pp')))
hc <- hc %>%
hc_annotations(list(xValue = 1, yValue = -8, title = list(text = '-5 pp')))
#2 - basically the same as #1
hc <- hc %>%
hc_annotations(list(xValue = 0, yValue = -2, title = list(text = '-6 pp'))) %>%
hc_annotations(list(xValue = 1, yValue = -8, title = list(text = '-5 pp')))
#3
hc <- hc %>%
hc_annotations(list(list(xValue = 0, yValue = -2, title = list(text = '-6 pp')),
list(xValue = 0, yValue = -8, title = list(text = '-5 pp')))
)
hc
我只是在这里使用了两个注释作为展示。在我的最终代码中,每一列下面都需要一个注释。
这是一个已知问题,已通过下一个函数解决:
hc_add_annotation
hc_add_annotations
使用示例:
hc %>%
hc_add_annotation(xValue = 0, yValue = -2, title = list(text = '-6 pp')) %>%
hc_add_annotation(xValue = 1, yValue = -4.5, title = list(text = '-5 pp'))
hc %>%
hc_add_annotations(
list(
list(xValue = 0, yValue = -2, title = list(text = '-6 pp')),
list(xValue = 1, yValue = -4.5, title = list(text = '-5 pp'))
)
)
甚至更好:
df <- data_frame(
xValue = c(0, 1),
yValue = c(-2, -4.5),
title = c("-6pp", "-5pp")
)
df <- df %>%
mutate(title = map(title, function(x) list(text = x)))
df
#> # A tibble: 2 x 3
#> xValue yValue title
#> <dbl> <dbl> <list>
#> 1 0 -2.0 <list [1]>
#> 2 1 -4.5 <list [1]>
hc %>%
hc_add_annotations(df)