This is the solution that best fits my needs.

Thanks everyone for their responses,
Naresh

Sent from my iPhone

On May 6, 2022, at 2:12 PM, David Carlson 
<dcarl...@tamu.edu<mailto:dcarl...@tamu.edu>> wrote:

You can't get exactly what you want with base graphics, but you can get close 
by defining line types and colors outside the plot command:

x <- seq(-3, 3, by = 0.01)
lns <- 1:2
clr <- 1:2
matplot(x, cbind(x, x^2), type="l", lty=lns, col=clr)
legend("bottomright", legend = c("x", expression(x^2)), lty = lns, col = clr, 
bty = "n")

David L Carlson

legend("bottomright", legend = c("x", expression(x^2)), lty = lns, col = clr, 
bty = "n")


On Thu, May 5, 2022 at 4:56 PM Jim Lemon 
<drjimle...@gmail.com<mailto:drjimle...@gmail.com>> wrote:
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_gurbux...@hotmail.com<mailto:naresh_gurbux...@hotmail.com>> wrote: >
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.

ZjQcmQRYFpfptBannerEnd

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_gurbux...@hotmail.com<mailto:naresh_gurbux...@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@r-project.org<mailto:R-help@r-project.org> mailing list -- To 
> UNSUBSCRIBE and more, see
> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!EYg6PPgiIfAXtUtQ0ynC6qvjRRReVyXbg66szoAq-KGtyrB_Aw3y2jP6nK_opXOaTsRO6SIjfuBoYZCPlaA$
> PLEASE do read the posting guide 
> https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!EYg6PPgiIfAXtUtQ0ynC6qvjRRReVyXbg66szoAq-KGtyrB_Aw3y2jP6nK_opXOaTsRO6SIjfuBoIIlYb4A$
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To 
UNSUBSCRIBE and more, see
https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!EYg6PPgiIfAXtUtQ0ynC6qvjRRReVyXbg66szoAq-KGtyrB_Aw3y2jP6nK_opXOaTsRO6SIjfuBoYZCPlaA$
PLEASE do read the posting guide 
https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!EYg6PPgiIfAXtUtQ0ynC6qvjRRReVyXbg66szoAq-KGtyrB_Aw3y2jP6nK_opXOaTsRO6SIjfuBoIIlYb4A$
and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to