Is it possible to define legend in the plot command? ?That will ensure that legend is correctly associated with the points or lines in the plot.? # I can do this x <- seq(-3, 3, by = 0.01) plot(x, x, type = "l") lines(x, x^2, lty = 2, col = 2) # In legend command, I need to remember that x^2 is lty = 2 and col = 2? legend("bottomright", legend = c("x", expression(x^2)), lty = c(1, 2), col = c(1, 2), bty = "n") # I want to do something like this plot(x, x, type = "l", label = "x") lines(x, x^2, lty = 2, col = 2, label = expression(x^2)) # legend command already knows the legend for each line legend("bottomright", bty = "n") Thanks, Naresh
Hi. AFAIK in regular plot it is not possible and you need to use x <- seq(-3, 3, by = 0.01) plot(x, x, type = "l") lines(x, x^2, lty = 2, col = 2) legend("bottomright", legend = c("x", expression(x^2)), lty = c(1, 2), col c(1, 2), bty = "n") But you could use ggplot2 package with slightly modified data # just prepare data dat <- data.frame(x=x, y=x, z=x^2) library(tidyr) datm <- pivot_longer(dat, 2:3) datm <- as.data.frame(datm) names(datm)[2] <- "type" datm$type <-factor(datm$type, labels=c("x", "x^2")) #make plot library(ggplot2) p <- ggplot(datm, aes(x, value, colour=type) ) p+geom_line() Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Naresh > Gurbuxani > Sent: Thursday, May 5, 2022 2:41 PM > To: r-help at r-project.org > Subject: [R] legend in plot > > Is it possible to define legend in the plot command? ?That will ensurethat> legend is correctly associated with the points or lines in the plot. > > # I can do this > x <- seq(-3, 3, by = 0.01) > plot(x, x, type = "l") > lines(x, x^2, lty = 2, col = 2) > # In legend command, I need to remember that x^2 is lty = 2 and col = 2 > legend("bottomright", legend = c("x", expression(x^2)), lty = c(1, 2), col= c(1,> 2), bty = "n") > > # I want to do something like this > plot(x, x, type = "l", label = "x") > lines(x, x^2, lty = 2, col = 2, label = expression(x^2)) > # legend command already knows the legend for each line > legend("bottomright", bty = "n") > > Thanks, > Naresh > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi Naresh, Have a look at the emptyspace function in the plotrix package. This returns the center of the largest empty space on the plot. Jim On Thu, May 5, 2022 at 10:41 PM Naresh Gurbuxani <naresh_gurbuxani at hotmail.com> wrote:> > Is it possible to define legend in the plot command? That will ensure that legend is correctly associated with the points or lines in the plot. > > # I can do this > x <- seq(-3, 3, by = 0.01) > plot(x, x, type = "l") > lines(x, x^2, lty = 2, col = 2) > # In legend command, I need to remember that x^2 is lty = 2 and col = 2 > legend("bottomright", legend = c("x", expression(x^2)), lty = c(1, 2), col = c(1, 2), bty = "n") > > # I want to do something like this > plot(x, x, type = "l", label = "x") > lines(x, x^2, lty = 2, col = 2, label = expression(x^2)) > # legend command already knows the legend for each line > legend("bottomright", bty = "n") > > Thanks, > Naresh > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.