Ted
Ted

Reputation: 105

How do I zoom in through ggplot2-autoplot with time series data

I have a set of multivariate time series data. I managed to use the package of ggplot2 and ggfortify to plot the time series data. However, I would like to zoom to see specific time series.

Method1: The best way is if there are any package that allow me to zoom in the plot through clicking, similar to the package zoom?

Method2: Another simpler way would be to limit the x axis for me to view certain time series data instead.

This is my plot, however I would like to zoom to view more specific data if possible. Method 1 would be more preferred if possible.

Time series data

This are some samples of my data:

     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

These are the code to make it my plot above:

library("ggplot2")
library("ggfortify")
autoplot(data,facets=FALSE)

I have tried to edit the code slightly to fix the xlim, however it doesnt work. The same plot above is plot out.

autoplot(QuantityEThailand,facets=TRUE,xlim=(c(as.Date("2015-01-01", "2015-05-01"))))

This is the documentation of autoplot of the time-series. ggfortify - autoplot.ts

Upvotes: 2

Views: 3743

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 269854

These are not ggplot2 but they are pretty simple and perhaps they will do. First get some input data:

library(quantmod)
getSymbols("SPY")

1) dygraphs Now try this:

library(dygraphs)
dygraph(Cl(SPY))

Drag your mouse across the portion that you want zoomed.

2) quantmod Here is another approach. It uses quantmod which we already loaded above.

chartSeries(SPY)
zoomChart("2015:")

Upvotes: 3

Maksim Gayduk
Maksim Gayduk

Reputation: 1082

In order to make it possible to "zoom in" the plot, you will have to add some sort of interactivity to your code. It is impossible in ggplot itself (unless you use some other package on top of it, like {manipulate}, {shiny} etc). Easiest way to do it is through plotly, because it can work directly with your ggplot objects. You should check installation guide here: https://plot.ly/r/getting-started/

After that you just do the following:

 library(plotly)
 py<-plotly()
 autoplot(data,facets=FALSE)
 py$ggplotly()

That is the easiest way to achieve interactivity. The plot will appear online on plot.ly website, and it will allow you to zoom in and do other interactive stuff.

Upvotes: 0

timelyportfolio
timelyportfolio

Reputation: 6579

I'm not sure I understand the question or anticipated result, but perhaps this will help.

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()

Upvotes: 0

Related Questions