在 R 的 Dygraphs 中突出显示折线图上的特定点

Highlight specific points on line charts in Dygraphs for R

是否可以在 R 中使用 dygraphs 突出显示折线图上的特定点,而指针不在该点?

这可以在其他绘图选项中通过同时绘制折线图和散点图来实现。但是,由于在 dygraphs 中散点图不可用,是否有任何解决方法?

在 dygraphs 图中突出显示特定点的方法是使用函数 dyAnnotation()。这是一个示例 R 代码:

library(xts)
library(dygraphs)
# convert numeric time index of ldeaths into class 'Date' (approximately)
in_dex <- as.Date(365*(zoo::index(ldeaths)-1970))
# convert time index from class 'Date' to 'POSIXct'
in_dex <- as.POSIXct.Date(in_dex)

# convert ldeaths into xts time series
l_deaths <- xts::xts(as.numeric(ldeaths), order.by=in_dex)
# calculate number of years
n_years <- NROW(in_dex)/12
# calculate the January dates
jan_dates <- in_dex[1 + 12*(0:(n_years - 1))]
# calculate the July dates
jul_dates <- in_dex[7 + 12*(0:(NROW(in_dex)/12 - 1))]

# create dygraph object
dy_graph <- dygraphs::dygraph(l_deaths, main="Dygraph of ldeaths with Annotations") %>% 
    dyHighlight(highlightCircleSize=5)

# add annotations for the January and July dates to dygraph object
for (i in 1:NROW(jan_dates)) {
  dy_graph <- dygraphs::dyAnnotation(dy_graph, x=jan_dates[i], text="Jan")
}  # end for
for (i in 1:NROW(jul_dates)) {
  dy_graph <- dygraphs::dyAnnotation(dy_graph, x=jul_dates[i], text="Jul")
}  # end for

# plot dygraph object
dy_graph

上面的 R 代码生成了以下 dygraphs 图: