How to add multiple straight lines to multi plot.zoo

I have several graphs of time series data, and I need a horizontal line on each graph, but with different horizontal values ​​(for example, 1st graph: h=50 , 2nd graph: h=48 ...).

I tried abline(h=50... and I get a horizontal line in each graph. I tried abline(h=c(50,48... and I get multiline horizontal lines on each graph.

I cannot figure out how to get the index plot.zoo in order to build h=50 in the 1st plot, h=48 in the second plot, etc.

 library(xts) data(sample_matrix) x <- as.xts(sample_matrix) # plot with single line my.panel <- function(x, ...) { lines(x, ...) abline(h=50, col = "red", lty="solid", lwd=1.5 ) } plot.zoo(x, main="title", plot.type="multiple", type="o", lwd=1.5, col="blue", panel=my.panel) # plot multiple lines in all plots my.panel <- function(x, ...) { lines(x, ...) abline(h=c(50,50,48,50), col = "red", lty="solid", lwd=1.5 )} plot.zoo(x, main="title", plot.type="multiple", type="o", lwd=1.5, col="blue", panel=my.panel) 
+8
r plot zoo
source share
1 answer

Customization of individual panels in multi-channel graphics is not described in detail in the actual text ?plot.zoo . In the "Details" section you will find:
"In the case of a custom panel, panel can refer to parent.frame$panel.number to determine which frame the panel is called from. See Examples.". And there are many examples. Using them as a template, I found that this could be a way to invoke separate panels and draw a separate hline in each.
Update Thanks @G. Grothendieck for editing, which made the code a lot cleaner!

 # create values for hline, one for each panel hlines <- c(50, 50, 48, 50) # panel function that loops over panels my.panel <- function(x, ...) { lines(x, ...) panel.number <- parent.frame()$panel.number abline(h = hlines[panel.number], col = "red", lty = "solid", lwd = 1.5) } plot.zoo(x, main = "title", type = "o", lwd = 1.5, col = "blue", panel = my.panel) 

enter image description here

+6
source share

All Articles