0
我有四年的時間序列數據。現在我想要逐年繪製相同的數據並進行比較分析。將假數據是使用ggplot繪製長時間序列數據
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)
現在,當我使用ggplot時間過久,使用以下行
visualize_dataframe_all_columns(xts_df)
繪製所有系列中的上述函數定義爲:
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)
}
以上方法的問題是:
- 繪圖需要很長時間。我可以縮短繪圖時間嗎?
- 使用
plotly
來放大繪圖非常困難。有沒有其他更好的方法
有沒有更好的方法來可視化這些數據?