0
votes

I have time-series data of four years. Now I want to plot the same data year-wise and do comparative analysis. The dummy data is as

library(xts)
library(ggplot2)
timeindex <- seq(as.POSIXct('2016-01-01'),as.POSIXct('2016-12-31 23:59:59'), by = "1 mins")
dataframe <- data.frame(year1=rnorm(length(timeindex),100,10),year2=rnorm(length(timeindex),150,7),
                        year3=rnorm(length(timeindex),200,3),
                        year4=rnorm(length(timeindex),350,4))
xts_df <- xts(dataframe,timeindex) 

Now, when I use ggplot it takes too long to plot all the series using following lines

visualize_dataframe_all_columns(xts_df)

The above function is defined as:

visualize_dataframe_all_columns <- function(xts_data) {
  library(RColorBrewer)# to increase no. of colors
  library(plotly)
  dframe <- data.frame(timeindex=index(xts_data),coredata(xts_data))
  df_long <- reshape2::melt(dframe,id.vars = "timeindex")
  colourCount = length(unique(df_long$variable))
  getPalette = colorRampPalette(brewer.pal(8, "Dark2"))(colourCount) # brewer.pal(8, "Dark2") or brewer.pal(9, "Set1")
  g <- ggplot(df_long,aes(timeindex,value,col=variable,group=variable))
  g <- g + geom_line() + scale_colour_manual(values=getPalette)
  ggplotly(g)
}

Problems with above approach are:

  1. It takes long time to plot. Can I reduce the plot time?
  2. It is very diffcult to zoom into the plot using plotly. Is there any other better way

Are there any better approaches to visualize this data?

1

1 Answers

0
votes

I faced more or less the same problem with frequency of 10 mins data. However, the question is that, does it make sense to plot the minute data for whole year? Human eyes cannot recognize the difference.

I would create a daily xts from that data and and plot it for the year. And modify the function to plot for a period of time for the minute data.