Plot 2 xts time series in one plot - r

Hi I thought this would be a simple task, 2hrs later and I am still struggling.
I was able with this code
chartSeries(snp.obj, TA=c("addTA(over,layout=NULL)"))
However it comes with a 2 paned plot but I am looking for these two xts objects overlayed with different y-axis to be in one plot not like I have it in the chartSeries plot.
Answer that works but possibly not so elegant:
over = xts(over, order.by=snp.obj[121:1730])
plot(snp.obj, main='Shiller PE Timer')
lines(2000*over+1, col= 'red')`

Use the on= argument to addTA (see ?addTA)
chartSeries(snp.obj, TA=c("addTA(over, on=1)"))

Related

Lag 1 Scatter Plot in R

I have an assignment. There are two datasets and in one of the questions, I needed to find the lag 1 scatter plot of each time series.
I created the graphs, but my question is what does it tell us? I know we are looking for some sort of autocorrelation, yet I couldn't properly understand it. What can we infer from this scatter plot and how can we make any inference?
To give you a better understanding I added one of the dataset and its graph, as well.
Edit
Data and graph code.
x <- c(39,35,16,18,7,22,13,18,20,9,-12,-11,-19,-9,-2,16)
plot(dplyr::lag(x), x)
The graph can also be plotted with
tsDyn::autopairs(x, type = "points")

Julia - Displaying several plots in the same plot (not subplot)

Plotting several series in a same plot display is possible and also several subplots in a display. But I want several plots which can be completely different things (not necessarily a series or graph of a map) to be displayed exactly in one frame. How can I do that? In Maple you assign names for each plot like
P1:=...:, P2:= ...: and then using plots:-display(P1,P2,...); and it works. But I want to do this in Julia. Let's say I have the following plots as an example;
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
plot(x,y)
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :yellow))
Now how to have both P1 and P2 in one plot? I don't one a shortcut or trick to write the output of this specific example with one plot line, note that my question is general, for example p2 can be a curve or something else, or I may have a forflow which generates a plot in each step and then I want to put all those shapes in one plot display at the end of the for loop.
Code for a simple example of trying to use plot!() for adding to a plot with arbitrary order.
using Plots
pyplot()
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange))
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot!(x2,y2,fill=(0, :orange))
p3=plot(x,y)
display(p2)
p5=plot!([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green))
By running the above code I see the following plots respectively.
But what I expected to see is a plot with the green rectangle added inside the plot with the two orange rectangles.
The way to plot several series within the same set of axes is with the plot! function. Note the exclamation mark! It's part of the function name. While plot creates a new plot each time it is invoked, plot! will add the series to the current plot. Example:
plot(x, y)
plot!(x, z)
And if you are creating several plots at once, you can name them and refer to them in plot!:
p1 = plot(x, y)
plot!(p1, x, z)
Well, if you do that, what you will have is subplots, technically. That's what it means.
The syntax is
plot(p1, p2)
Sorry, I don't know how to plot a whole plot (conversely to a series) over an other plot.. For what it concerns the order of the plots, you can create as many plots as you want without display them and then display them wherever you want, e.g.:
using Plots
pyplot()
# Here we create independent plots, without displaying them:
x=[1,2,2,1,1]
y=[1,1,2,2,1]
p1=plot(x,y,fill=(0, :orange));
x2=[2,3,3,2,2]
y2=[2,2,3,3,2]
p2=plot(x2,y2,fill=(0, :orange));
p3=plot(x,y);
p5=plot([1,2,2,1,1],[2,2,3,3,2],fill=(0, :green));
# Here we display the plots (in the order we want):
println("P2:")
display(p2)
println("P3:")
display(p3)
println("P5:")
display(p5)
println("P1:")
display(p1)

Why are all relevant tick-marks not plotted on the X-axis?

I often determine that when plotting in R not all relevant tick-marks are drawn. Relevant here means that there is data present.
See this example
> set.seed(NULL)
> d <- data.frame(a=sample(1:10, replace=TRUE), b=sample(11:30))
> plot(d)
The resulting plot where you can see values on the X-axis at 3, 5, 7 and 9. But the tick-marks for them are missing.
The focus of my question is to understand why R acts like that. What is the algorithm and logic behind it?
btw: I know how to solve it. I can draw the X-axis myself. But that is not part of the question.
You could find a brief description of the algorithm for plotting the tick marks using?axis.
plot() is a generic function to plot a wide sort of data. In your example, you are using discrete data. For continuous data, it does not make much sense to have a single tick mark for every single value, which would make unreadable the axes.
However, you can easily adjust the ticks in your plot using axis()

Plotting xts objects works with points but not with lines

I have merged two xts objects and want to plot them in a single display. This works fine when I use points (type="p"). However, when I use lines (type="l") a problem occurs: the first series is shown only in the index region that is not covered by the second series. I would expect the lines to be as long as the "points". A reproducible example is posted below.
As this occurs with both the default and the ggplot plotting commands, I suspect that this relates to some property of time-series data.
What is the reason for this behaviour? Is there a proper way of plotting this kind of data?
## Minimal example for Reproduction
library(xts)
library(ggplot)
# create two artificial xts objects
xts1 <- xts(1:15,Sys.Date()+10+seq(from=1,by=5,length.out=15))
xts2 <- xts(1:20,Sys.Date()+seq(from=1,by=2,length.out=20))
# merge them
merged.xts <- merge.xts(xts1,xts2)
# Plot as zoo objects to allow for panels
# plotting with points shows both series
plot(as.zoo(merged.xts),type="p",plot.type="single")
# plotting with lines
# The second series is "shortened"
plot(as.zoo(merged.xts),type="l",plot.type="single")
# Similar behaviour with ggplot2
autoplot(merged.xts)
Quite simply, type="l" looks the way it does because you can't plot a line on a single point. Set type="b" to see both lines and points.

for loop generates multiple plots

I have a question about the for loop in combination with the plot function.
I want to use a for loop function (see below) to plot multiple points in one plot. But my loop generates for each point his one plot. So with an i of 35 I generate 35 plot. My question is, is there a way to plot all the points in the same plot?
pdf("test plot.pdf")
for (i in 1:nrow(MYC)){
plot(MYC[i,1], MYC[i,2]
}
dev.off()
Thank you all!
As mentioned in the comments, you are in essence trying to do multiple plots with a loop. R doesn't understand that actually want to plot only points. There's a cure for that, and it comes in vials of points(). Before calling a loop, construct your plot using the type argument. This will make an empty plot, something along the lines of:
plot(your.data, type = "n")
You can then use your loop (with points) to add points to this existing plot.

Resources