[R] Howto? plot legend with no line behind the points
Martin Maechler
maechler at stat.math.ethz.ch
Mon Nov 21 16:33:09 CET 2005
>>>>> "Jan" == Jan Verbesselt <Jan.Verbesselt at biw.kuleuven.be>
>>>>> on Mon, 21 Nov 2005 12:28:35 +0100 writes:
Jan> Hi R-help,
Jan> We are using R 2.2 on Win XP and have a detail question on how the
Jan> legend can be optimised.
Jan> We use the following;
-> plot(,type="b",...)
Jan> The lines in the plot do not cross the points. How can we obtain the
Jan> same effect in the legend? (points without a line through them..)
not directly, however you can trick it, see below
Jan> We tried setting the pt.bg to white but this did not help.
I think it does help some.. read on
Jan> See script below.
par(mar=c(5,5,2,4) +.1) # dit om extra text in Y-as te plaatsen
plot(ts.X, type="b", col=1, pch=19,ylim=c(-0.4,0.2),ylab=c("X"))
legend.txt <- c("X","Y")
# Define how the legend looks like, place it on the right location
legend("topright", legend.txt,col=1,lty=1, pch=c(19,1),bty="n", pt.bg=1)
par(new=T)
plot(ts.Y, type="b", lty=1, col=1, pch=1, ylab="",
xlab="",yaxt="n",ylim=c(0.1,0.7))
axis(4)
mtext(side=4, line=3, "Y", cex=1)
Unfortunately you don't give a reproducible example {we don't
have your ts.X and ts.Y}, so I make up a simpler version of
the above:
## A bivariate time-series
set.seed(1)
xy <- cbind(x = ts(cumsum(rnorm(47))), y = ts(cumsum(rt(47, df=3))))
plot(xy, plot.type = "single", type = "b", pch = 21, col=1:2, bg = "light blue")
legend("topright", c("x","y"), col=1:2, lty="47",
pch = 21, bty="n", pt.bg="light blue")
I'm using pch=21 and I've used pt.bg just to show it's effect
here; of course you can play with these as well.
And the real trick was to "play with" 'lty' using the nice "on/off"
convention ("47": a dash of length 4; a break of length 7).
Now of course, we also might accept patches for improving
legend (source at
https://svn.R-project.org/R/trunk/src/library/graphics/R/legend.R ).
Naturally, I think it's the argument 'merge' that
could be extended to allow something analogous to plot type = "b".
Hoping this helps:
Martin Maechler, ETH Zurich
More information about the R-help
mailing list