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

时间:2016-01-28 09:37:22

标签: r dygraphs

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

通过绘制折线图和散点图,可以在其他图形选项中实现。然而,因为在dygraphs散点图不可用是否有任何解决方法?

1 个答案:

答案 0 :(得分:1)

突出显示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图: enter image description here