Closed crossxwill closed 1 year ago
I just noticed I never answered you. I'm sorry. Your solution is one way to do it. Another is to (re-)assign the plot object to a variable as you add elements, and only print()
the plot when you've added everything.
data(USAccDeaths)
USAccDeaths <- as.xts(USAccDeaths)
p <- plot.xts(USAccDeaths)
p <- addLegend("topleft", legend.names=c("Deaths"), col=c("black"), lty=1:2, cex=0.8, on=0)
p <- addLegend("topright", legend.names=c("AccDeaths"), col=c("black"), lty=1:2, cex=0.8, on=0)
print(p)
Your original code renders the plot 3 times because you call 3 plotting functions without assigning the result. R automatically calls print()
on an expression when it's evaluated and not assigned to anything.
I'm going to close this because plot.xts()
is behaving as expected. But feel free to ask follow-up questions.
Description
I want to generate two legends on a single plot object. However, using
addLegend(..., on=0)
renders a new plot object each timeaddLegend
is called.Expected behavior
Single plot object with two legends.
Minimal, reproducible example
Session Info
Work-around
Embed the
addLegend
commands as a vector of characters in thepanels
argument.