AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Plot multiple time series same graph r1/19/2024 ![]() ![]() Last, but crucial, is the parameter setting screens = 1, which tells the plot command to overlay each series in a single graph.įinally, we include the legend(.) command to place a color legend at the upper left hand corner of the graph. Next, in the plot(.) command, we assign to x our “matrix” of time series in the zoo.basket object, labels for the horizontal and vertical axes (xlab, ylab), a title for the graph (main), the the colors (col). This is a nice feature for the impatient or lazy among us (yes, guilty as charged) who don’t want to be bothered with picking out colors and just want to see the result right away. It is convenient as R will take in an arbitrary positive integer value and select a sequence of distinct colors up to the number specified. ![]() We started by setting a color scheme, using the rainbow(.) command that is included in the base R installation. # Set a legend in the upper left hand corner to match color to return series Plot(x = zoo.basket, ylab = “Cumulative Return”, main = “Cumulative Returns”, Let’s now look at the code and the resulting plot in the following example, and then explain what’s going on: The plot(.) function provided in zoo is very simple to use, as we can use the whole zoo.basket object as input, and the plot(.) function will overlay the time series and scale the vertical axis for us with the help of a single parameter setting, namely the screens parameter. Looking at head(zoo.basket) and tail(zoo.basket), we will get output that looks the same as what we got for the original xts basket object, as shown above the date to data mapping is preserved. The xts package is an extension of the zoo package, so coercing our xts object basket to a zoo object is a simple task: In particular, we will look at the plot(.) functions in both the zoo and xts packages. We will now look how we can plot all five series, overlayed on a single graph. This will not be of any significant consequence for demonstrating plotting functions, however. ![]() SPY.Close QQQ.Close GDX.Close DBO.Close VWO.Close We first obtain the data using quantmod, going back to January 2007: To start, let’s look at five sets of cumulative returns for the following ETF’s: As this information is not explicitly covered in the examples provided with xts and base R, this discussion may save you a bit of time. ![]() Today, we’ll look at some quick and easy methods for plotting overlays of multiple xts time series in a single graph. Last time, we included a couple of examples of plotting a single xts time series using the plot(.) function (ie, said function included in the xts package). By Daniel Hanson, QA Data Scientist, Revolution Analytics Introduction and Data Setup ![]()
0 Comments
Read More
Leave a Reply. |