Is it possible to highlight specific points on a line chart using dygraphs in R without the pointer being located at that point?
This can be achieved in other graphing options by plotting a line chart and a scatter plot together. However since in dygraphs scatter plot is not available is there any work around?
The way to highlight specific points on a dygraphs plot is by using the function dyAnnotation()
. Here is an example R
code:
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