Hello, I have difficulties combining boxes and lines in plot legend. I searched previous R-posts and found this (with no solution): http://tolstoy.newcastle.edu.au/R/help/06/07/30248.html. Is there a way to avoid boxes behind the line legends? x1 <- rnorm(100) x2 <- rnorm(100, 2) hist(x1, main = "", col = "orange",ylab = "density", xlab = "x", freq = F, density = 55, xlim = c(-2, 5), ylim = c(0, 0.5)) par(new = T) hist(x2, main = "", col = "green", ylab = "", xlab = "",axes = F, xlim = c(-2, 5), ylim = c(0, 0.5), density = 45, freq = F) abline(v = mean(x1), col = "orange", lty = 2, lwd = 2.5) abline(v = mean(x2), col = "green", lty = 2, lwd = 2.5) legend(3, 0.45, legend = c("x1", "x2", "mean(x1)", "mean(x2)"), col c("orange", "green"), fill=c("orange","green", 0, 0), lty = c(0, 0, 2, 2), merge = T) Thanks Lauri
Gabor Grothendieck
2007-Sep-10 06:17 UTC
[R] plot legend: combining filled boxes and lines
Check out: http://tolstoy.newcastle.edu.au/R/e2/help/07/05/16777.html On 9/10/07, Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote:> Hello, > > I have difficulties combining boxes and lines in plot legend. I > searched previous R-posts and found this (with no solution): > http://tolstoy.newcastle.edu.au/R/help/06/07/30248.html. Is there a > way to avoid boxes behind the line legends? > > x1 <- rnorm(100) > x2 <- rnorm(100, 2) > hist(x1, main = "", col = "orange",ylab = "density", xlab = "x", freq > = F, density = 55, xlim = c(-2, 5), ylim = c(0, 0.5)) > par(new = T) > hist(x2, main = "", col = "green", ylab = "", xlab = "",axes = F, xlim > = c(-2, 5), ylim = c(0, 0.5), density = 45, freq = F) > > abline(v = mean(x1), col = "orange", lty = 2, lwd = 2.5) > abline(v = mean(x2), col = "green", lty = 2, lwd = 2.5) > legend(3, 0.45, legend = c("x1", "x2", "mean(x1)", "mean(x2)"), col > c("orange", "green"), fill=c("orange","green", 0, 0), lty = c(0, 0, > 2, 2), merge = T) > > Thanks > Lauri > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Thanks Gabor, I got it! For example: x1 <- rnorm(100) x2 <- rnorm(100, 2) hist(x1, main = "", col = "orange",ylab = "density", xlab = "x", freq = F, density = 55, xlim = c(-2, 5), ylim = c(0, 0.5)) par(new = T) hist(x2, main = "", col = "green", ylab = "", xlab = "",axes = F, xlim = c(-2, 5), ylim = c(0, 0.5), density = 45, freq = F) abline(v = mean(x1), col = "orange", lty = 2, lwd = 2.5) abline(v = mean(x2), col = "green", lty = 2, lwd = 2.5) legend(3, 0.45, legend = c("x1", "x2", "mean(x1)", "mean(x2)"), col = rep(c("orange", "green"), 2), pch=c(15,15, NA, NA), pt.cex=2, lty = c(0, 0, 2, 2)) -Lauri 2007/9/10, Gabor Grothendieck <ggrothendieck at gmail.com>:> Check out: > http://tolstoy.newcastle.edu.au/R/e2/help/07/05/16777.html > > On 9/10/07, Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote: > > Hello, > > > > I have difficulties combining boxes and lines in plot legend. I > > searched previous R-posts and found this (with no solution): > > http://tolstoy.newcastle.edu.au/R/help/06/07/30248.html. Is there a > > way to avoid boxes behind the line legends? > > > > x1 <- rnorm(100) > > x2 <- rnorm(100, 2) > > hist(x1, main = "", col = "orange",ylab = "density", xlab = "x", freq > > = F, density = 55, xlim = c(-2, 5), ylim = c(0, 0.5)) > > par(new = T) > > hist(x2, main = "", col = "green", ylab = "", xlab = "",axes = F, xlim > > = c(-2, 5), ylim = c(0, 0.5), density = 45, freq = F) > > > > abline(v = mean(x1), col = "orange", lty = 2, lwd = 2.5) > > abline(v = mean(x2), col = "green", lty = 2, lwd = 2.5) > > legend(3, 0.45, legend = c("x1", "x2", "mean(x1)", "mean(x2)"), col > > c("orange", "green"), fill=c("orange","green", 0, 0), lty = c(0, 0, > > 2, 2), merge = T) > > > > Thanks > > Lauri > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > >
This is not quite what you want but you can try this: legend(3, 0.45, legend = c("x1", "x2", "mean(x1)", "mean(x2)"), col c("orange", "green"),pch = c(15,15,-1,-1), lty=c(-1,-1,2,2)) Although pch=22 should draw a filled square with a border - but it draws only the border instead .... Monica ______________________________________________________________ Message: 24Date: Mon, 10 Sep 2007 09:05:27 +0300From: "Lauri Nikkinen" <lauri.nikkinen@iki.fi>Subject: [R] plot legend: combining filled boxes and linesTo: r-help@stat.math.ethz.chMessage-ID:<ba8c09910709092305y2e30640bg9b6dea975c589240@mail.gmail.com>Content-Type: text/plain; charset=ISO-8859-1 Hello, I have difficulties combining boxes and lines in plot legend. Isearched previous R-posts and found this (with no solution):http://tolstoy.newcastle.edu.au/R/help/06/07/30248.html. Is there away to avoid boxes behind the line legends? x1 <- rnorm(100)x2 <- rnorm(100, 2)hist(x1, main = "", col = "orange",ylab = "density", xlab = "x", freq= F, density = 55, xlim = c(-2, 5), ylim = c(0, 0.5))par(new = T)hist(x2, main = "", col = "green", ylab = "", xlab = "",axes = F, xlim= c(-2, 5), ylim = c(0, 0.5), density = 45, freq = F) abline(v = mean(x1), col = "orange", lty = 2, lwd = 2.5)abline(v = mean(x2), col = "green", lty = 2, lwd = 2.5)legend(3, 0.45, legend = c("x1", "x2", "mean(x1)", "mean(x2)"), col =c("orange", "green"), fill=c("orange","green", 0, 0), lty = c(0, 0,2, 2), merge = T) ThanksLauri _________________________________________________________________ Gear up for Halo® 3 with free downloads and an exclusive offer. It’s our way of saying thanks for using Windows Live™. [[alternative HTML version deleted]]