如何使用时间序列数据通过 ggplot2-autoplot 进行放大
How do I zoom in through ggplot2-autoplot with time series data
我有一组多变量时间序列数据。我设法使用 ggplot2 和 ggfortify 的包来绘制时间序列数据。但是,我想放大查看具体的时间序列。
方法一:
最好的方法是有没有什么包可以让我通过点击放大情节,类似于包 zoom?
方法2:另一种更简单的方法是限制x轴让我查看某些时间序列数据。
这是我的情节,但如果可能的话,我想放大以查看更具体的数据。如果可能的话,方法一会更受欢迎。
这是我的一些数据样本:
Philippines Nigeria Benin Senegal Malaysia South.Africa Japan Cameroon Sierra.Leone
[1,] 1104000 4000000 25500000 77867536 47606000 20269800 5035981 161724 23250
[2,] 1402900 61100000 51420000 82652160 55160000 30623720 5225030 288000 0
[3,] 1385000 0 53249884 35631284 25007000 2261000 30260000 12821859 21500
[4,] 6530330 21499050 100470000 108419264 23088000 35571248 16017590 2138612 92200
[5,] 22306400 29459750 52308248 43510000 53553600 20966730 40988640 24000000 46450
[6,] 75769744 4490000 80569432 59912600 24325500 79226104 11402132 255612 0
这些是使它成为我上面的情节的代码:
library("ggplot2")
library("ggfortify")
autoplot(data,facets=FALSE)
我尝试稍微编辑代码以修复 xlim,但它不起作用。上面同样的情节是画出来的。
autoplot(QuantityEThailand,facets=TRUE,xlim=(c(as.Date("2015-01-01", "2015-05-01"))))
这是时间序列自动绘图的文档。
ggfortify - autoplot.ts
我不确定我是否理解问题或预期结果,但这也许会有所帮助。
library(ggplot2)
library(xts)
data(sample_matrix)
test_xts <- as.xts(sample_matrix)
# look at autoplot results
autoplot( test_xts )
# to look at one series
autoplot( test_xts[,1] )
# to limit the x axis, probably best to do
# through ?subset.xts
autoplot( test_xts["2000::2007-03",1] )
# when working with xts/zoo and ggplot2/lattice
# best to convert to long form data.frame so you can
# customize and control
library( tidyr )
test_df <- gather(
data.frame(
date = index(test_xts)
,test_xts
)
,series
,value
,-date
)
ggplot( test_df, aes(x = date, y = value, color = series)) + geom_line()
为了能够 "zoom in" 情节,您必须在代码中添加某种交互性。这在 ggplot 本身是不可能的(除非你在它上面使用其他包,比如 {manipulate},{shiny} 等)。
最简单的方法是通过 plotly,因为它可以直接与您的 ggplot 对象一起使用。
您应该在此处查看安装指南:https://plot.ly/r/getting-started/
之后您只需执行以下操作:
library(plotly)
py<-plotly()
autoplot(data,facets=FALSE)
py$ggplotly()
这是实现交互最简单的方法。该情节将出现在 plot.ly 网站上,您可以放大并进行其他互动。
这些不是 ggplot2,但它们非常简单,也许可以。首先获取一些输入数据:
library(quantmod)
getSymbols("SPY")
1) dygraphs 现在试试这个:
library(dygraphs)
dygraph(Cl(SPY))
将鼠标拖过要缩放的部分。
2) quantmod 这是另一种方法。它使用我们已经在上面加载的 quantmod。
chartSeries(SPY)
zoomChart("2015:")
我有一组多变量时间序列数据。我设法使用 ggplot2 和 ggfortify 的包来绘制时间序列数据。但是,我想放大查看具体的时间序列。
方法一: 最好的方法是有没有什么包可以让我通过点击放大情节,类似于包 zoom?
方法2:另一种更简单的方法是限制x轴让我查看某些时间序列数据。
这是我的情节,但如果可能的话,我想放大以查看更具体的数据。如果可能的话,方法一会更受欢迎。
这是我的一些数据样本:
Philippines Nigeria Benin Senegal Malaysia South.Africa Japan Cameroon Sierra.Leone
[1,] 1104000 4000000 25500000 77867536 47606000 20269800 5035981 161724 23250
[2,] 1402900 61100000 51420000 82652160 55160000 30623720 5225030 288000 0
[3,] 1385000 0 53249884 35631284 25007000 2261000 30260000 12821859 21500
[4,] 6530330 21499050 100470000 108419264 23088000 35571248 16017590 2138612 92200
[5,] 22306400 29459750 52308248 43510000 53553600 20966730 40988640 24000000 46450
[6,] 75769744 4490000 80569432 59912600 24325500 79226104 11402132 255612 0
这些是使它成为我上面的情节的代码:
library("ggplot2")
library("ggfortify")
autoplot(data,facets=FALSE)
我尝试稍微编辑代码以修复 xlim,但它不起作用。上面同样的情节是画出来的。
autoplot(QuantityEThailand,facets=TRUE,xlim=(c(as.Date("2015-01-01", "2015-05-01"))))
这是时间序列自动绘图的文档。 ggfortify - autoplot.ts
我不确定我是否理解问题或预期结果,但这也许会有所帮助。
library(ggplot2)
library(xts)
data(sample_matrix)
test_xts <- as.xts(sample_matrix)
# look at autoplot results
autoplot( test_xts )
# to look at one series
autoplot( test_xts[,1] )
# to limit the x axis, probably best to do
# through ?subset.xts
autoplot( test_xts["2000::2007-03",1] )
# when working with xts/zoo and ggplot2/lattice
# best to convert to long form data.frame so you can
# customize and control
library( tidyr )
test_df <- gather(
data.frame(
date = index(test_xts)
,test_xts
)
,series
,value
,-date
)
ggplot( test_df, aes(x = date, y = value, color = series)) + geom_line()
为了能够 "zoom in" 情节,您必须在代码中添加某种交互性。这在 ggplot 本身是不可能的(除非你在它上面使用其他包,比如 {manipulate},{shiny} 等)。 最简单的方法是通过 plotly,因为它可以直接与您的 ggplot 对象一起使用。 您应该在此处查看安装指南:https://plot.ly/r/getting-started/
之后您只需执行以下操作:
library(plotly)
py<-plotly()
autoplot(data,facets=FALSE)
py$ggplotly()
这是实现交互最简单的方法。该情节将出现在 plot.ly 网站上,您可以放大并进行其他互动。
这些不是 ggplot2,但它们非常简单,也许可以。首先获取一些输入数据:
library(quantmod)
getSymbols("SPY")
1) dygraphs 现在试试这个:
library(dygraphs)
dygraph(Cl(SPY))
将鼠标拖过要缩放的部分。
2) quantmod 这是另一种方法。它使用我们已经在上面加载的 quantmod。
chartSeries(SPY)
zoomChart("2015:")