Use Dygraph for R to Plot Xts Time Series by Year Only

Use dygraph for R to plot xts time series by year only?

This looks like a bug in as.xts.ts. It uses length(x) to create the sequence of dates for the index, which returns the number of elements for a matrix (not the number of rows).

You can work around it by using as.xts on your ts objects before calling cbind on them.

both <- cbind(men=as.xts(men), women=as.xts(women))

Dygraph on tibble converted to xts, with 3001 series

Well, what I've accomplished after some time is that the first attempt using tbl_xts is the right way to separate the graph in individual lines, but then I use a filter to only create columns for the cities which are contained in the shiny input. So it's now really fast :)

Like:

xtsdata <- tbl_xts(dados %>% filter(city%in%dados$city), cols_to_xts = "totalCases", spread_by = "city")
g2 <- dygraph(xtsdata, xlab='Dia', ylab='Casos (acumulado)', main="Gráfico temporal")

R timeseries jumps and plotting in dygraph

Apparently there is no solution to hide it, other than going for NA values to hide the graph itself, but the gap which still exist.

I now went for a generated timestamp misused as index to simulate the effect of an ongoing graph.

R dygraphs - plot certain content of columns

Here are some approaches:

First, use dplyr to filter() the data ahead of time:

library(dplyr)
apples <- data %>%
filter(Fruit == "Apple")
apples_ts <- xts(apples$Count, order.by = apples$Date)
dygraph(apples_ts)

Second, subset the data in a more traditional way ahead of time.

apples <- data[which(data$Fruit == "Apples"),]
apples_ts <- xts(apples$Count, order.by = apples$Date)
dygraph(apples_ts)

Third, subset the data in the call to xts:

apples_ts <- xts(data[data$Fruit == "Apple",]$Count, 
order.by = data[data$Fruit == "Apple",]$Date)
dygraph(apples_ts)

Fourth, write a custom function so you don't need to repeat this code for each fruit:

fruit_dygraph <- function(data, fruit) {
fruit_ts <- xts(data[data$Fruit == fruit,]$Count,
order.by = data[data$Fruit == fruit,]$Date)
dygraph(fruit_ts)
}
fruit_dygraph(data, fruit = "Apple")


Related Topics



Leave a reply



Submit