0

I want to produce multiple graphs of a time series xts object in different windows. The issue is that I cannot add only one legend (for the last plot). My code is the following:

dev.new(width=3,height=9)
par(mfrow=c(3,1))
plot(csum_GVMP[,c(-2,-3)],main=" ",minor.ticks="years",cex.axis = 1,major.ticks="years",grid.ticks.on=FALSE,grid.ticks.lty=0,col=color)
addLegend("bottomleft",legend.names = c("","","","","","",""))
plot(csum_ERC[,c(-2,-3)],main=" ",minor.ticks="years",cex.axis = 1,major.ticks="years",grid.ticks.on=FALSE,grid.ticks.lty=0,col=color)
addLegend("bottomleft",legend.names = c("","","","","","",""))
plot(csum_MD[,c(-2,-3)],main=" ",minor.ticks="years",cex.axis = 1,major.ticks="years",grid.ticks.on=FALSE,grid.ticks.lty=0,col=color)

As you see I added blank values for the legend names for the 1st and 2nd plot, but the results is that the graphs are of the same plot are being repeated two times like these: showing only the plot for the csum_GVMP here enter image description here Otherwise if I leave the addLegend out the plot looks like this here, enter image description here

which is what I want but now I would like to add only one legend. If I leave out the command addLegend for 1st and 2nd plot, the figures are not even plotted. Does it anybody know how to handle this? Thank you in advance.

Prany
  • 2,078
  • 2
  • 13
  • 31
Ana B.
  • 1
  • 2
  • Could you please provide a [reproducible example](https://stackoverflow.com/q/5963269/271616)? That will help me (and others) debug the issue and provide a solution. – Joshua Ulrich Jun 10 '18 at 17:21

2 Answers2

0

here you go. If you uncomment the addLegend it will duplicate the graphs, as I mentioned in the post. I hope this helps

set.seed(10)
library(MASS)
library(xts)

date=seq(as.Date("2000/1/1"), as.Date("2000/1/10"), "days")
matrixA=as.numeric(mvrnorm(n = 30, 0.5, 0.2, tol = 1e-6, empirical = TRUE, EISPACK = FALSE))
matrixA=matrix(matrixA,10,3)
martixA.ts=as.xts(matrixA,date)

matrixB=as.numeric(mvrnorm(n = 30, 0.5, 0.2, tol = 1e-6, empirical = TRUE, EISPACK = FALSE))
matrixB=matrix(matrixB,10,3)
martixB.ts=as.xts(matrixB,date)

par(mfrow=c(2,1))
plot(as.xts(matrixA,date),main="A")
#addLegend("bottomleft",legend.names = c("A","B"))
plot(as.xts(matrixB,date),main="B")
#addLegend("bottomleft",legend.names = c("",""))

You should be able to see this

enter image description here

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Ana B.
  • 1
  • 2
  • Is this a reply to my reproducible example comment, or an answer to your question? – Joshua Ulrich Jun 11 '18 at 00:07
  • This works perfectly thanks. One more question how do you disable displaying the time interval on the right upper corner of the figure? Thanks in advance. – Ana B. Jun 13 '18 at 09:13
  • See [this answer](https://stackoverflow.com/questions/50051076/changes-in-plotting-an-xts-object/50051183#50051183) – Joshua Ulrich Jun 15 '18 at 12:13
0

I'm not particularly happy with this solution, but it solves the immediate problem.

The strategy is to "build" the plot to completion before plotting/printing it. See below.

set.seed(10)
library(MASS)
library(xts)

date <- seq(as.Date("2000-01-01"), as.Date("2000-01-10"), "days")
matrixA <- matrix(mvrnorm(n = 30, 0.5, 0.2, empirical = TRUE), 10, 3)
matrixA.ts <- xts(matrixA, date)

matrixB <- matrix(mvrnorm(n = 30, 0.5, 0.2, empirical = TRUE), 10, 3)
matrixB.ts <- xts(matrixB, date)

# Create the first plot, but do not draw it
# Assign the result to 'p1'
p1 <- plot(matrixA.ts, main = "A")
p1 <- addLegend("bottomleft", legend.names = c("A","B"))

# Create the second plot without drawing it
# Assign the result to 'p2'
p2 <- plot(matrixB.ts, main = "B")
p2 <- addLegend("bottomleft", legend.names = c("",""))

# Set up the device layout, and draw both plots
par(mfrow=c(2,1))
p1
p2
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418