高图中的注释不能按预期工作

问题描述 投票:0回答:1

我正在使用Highchart绘制一些时间序列,并希望在绘图中添加一些注释以突出显示一些关键点。我知道将光标放在图形上可以弹出上下文,但是,需要一些自动图形生成,因此注释是最好的方法。

我这样做,使用下面代码中的最后一行。但是,效果不是我的预期。文本位于左下角,不位于右侧水平位置,垂直位置右侧。时间序列是使用xts库创建的,这意味着水平轴只是日期数据结构,没什么特别的。 xValue被指定为总长度为1018的所有时间点的第900个元素,因此第900个时间点必须位于图形的后半部分。

任何人都知道如何将注释放在正确的位置?非常感谢。

hc <- highchart(type = "stock") %>% 
  hc_title(text = "Some time series") %>% 
  hc_add_series(x, color='green', name="x", showInLegend = TRUE) %>%
  hc_add_series(y, color='red', name="y", showInLegend = TRUE) %>% 
  hc_add_series(z, color='blue', name="z", showInLegend = TRUE) %>% 
  hc_navigator(enabled=FALSE) %>% 
  hc_scrollbar(enabled=FALSE) %>%
  hc_legend(enabled=TRUE, layout="horizontal") %>%
  hc_annotations(list(enabledButtons=FALSE, xValue = index(x)[900], yValue = -5, title =list(text = "Hello world! How can I make this work!")))

hc

可以使用以下脚本粗略生成数据:

dt <- seq(as.Date("2014/1/30"), as.Date("2018/2/6"), "days")
dt <- dt[!weekdays(dt) %in% c("Saturday", "Sunday")]
n <- length(dt)
x <- xts(rnorm(n), order.by=dt)
y <- xts(rnorm(n), order.by=dt)
z <- xts(rnorm(n), order.by=dt)

Graph is here.

r date highcharts xts
1个回答
1
投票

让我们用@ kamil-kulig的例子来说明星,这将是R世界的一点点,但如果你不介意我想给出一些理由。

如果我们看到注释选项不是一个对象而是一个对象列表,那么在highcharter中实现了hc_add_annotation函数。

现在,您使用的是旧版本的highcharter。 Highcharter devlopment版本正在使用highchartsJS的v6进行了一些更改:在annotations.js之前,现在插入作为一个模块包含参数名称的一些更改。

示例I:简单

Kamil Kulig的例子被复制:

highchart(type = "stock") %>% 
  hc_add_annotation(
    labelOptions = list(
      backgroundColor = 'rgba(255,255,255,0.5)',
      verticalAlign = 'top',
      y = 15
    ),
    labels = list(
      list(
        point = list(
          xAxis = 0,
          yAxis = 0,
          x = datetime_to_timestamp(as.Date("2017/01/02")),
          y = 1.5
        ),
        text = "Some annotation"
      )
    )
  ) %>% 
  hc_xAxis(
    minRange = 1
  ) %>% 
  hc_add_series(
    pointStart = start,
    pointInterval = day,
    data = c(3, 4, 1)
  )

enter image description here

示例II:使用您的数据

小心添加x位置的方式。 Highcharter包含一个datetime_to_timestamp函数,用于将日期转换为highcharts所需的纪元/时间戳。

library(xts)

dt <- seq(as.Date("2014/1/30"), as.Date("2018/2/6"), "days")
dt <- dt[!weekdays(dt) %in% c("Saturday", "Sunday")]
n <- length(dt)
x <- xts(rnorm(n), order.by=dt)
y <- xts(rnorm(n), order.by=dt)
z <- xts(rnorm(n), order.by=dt)


highchart(type = "stock") %>% 
  hc_title(text = "Some time series") %>% 
  hc_add_series(x, color='green', name="x", showInLegend = TRUE) %>%
  hc_add_series(y, color='red', name="y", showInLegend = TRUE) %>% 
  hc_add_series(z, color='blue', name="z", showInLegend = TRUE) %>% 
  hc_navigator(enabled=FALSE) %>% 
  hc_scrollbar(enabled=FALSE) %>%
  hc_legend(enabled=TRUE, layout="horizontal") %>%
  hc_add_annotation(
    labels = list(
      list(
        point = list(
          xAxis = 0,
          yAxis = 0,
          x = datetime_to_timestamp(as.Date(index(x)[900])),
          y = 1
        ),
        text = "Hello world! How can I make this work!"
      )
    )
  )

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.