[R] Plot Legend

R. Michael Weylandt michael.weylandt at gmail.com
Tue Oct 25 00:38:59 CEST 2011


Glad to hear it worked for you.

There does seem to be some confusion on your end as to what the with()
command does, however. The following are all equivalent.

data(mtcars)
layout(matrix(1:4,2))

plot(mtcars$cyl, mtcars$mpg)
plot(mtcars[["cyl"]], mtcars[["mpg"]])
plot(mtcars[,"cyl"], mtcars[,"mpg"])
with(mtcars, plot(cyl, mpg))

In effect, with() lets you tell R, for this expression only, you can
look up column names as variables directly: for a long line, this just
clears the syntax up. If you've ever heard of the attach() command --
not recommended -- it's basically a nicely behaved local version
thereof.

If I'm reading your code correctly, it can all be written quite succinctly as:

with(dataset, {
plot ( Na, Kt, col = Drug, type = "p", pch = 20)
legend ("top", legend = unique (Drug), pch = unique(Drug))
legend ("bottom", legend = unique (Drug), pch =20, col =unique (Drug))
})

Though it seems like that second line isn't actually accurate to what
you plotted so you may with to remove it.

M

On Mon, Oct 24, 2011 at 4:30 PM, RMSOPS <ricardosousa2000 at clix.pt> wrote:
> Hello Michael,
>
>     You were absolutely right, it was not the database mtcars I wanted to
> use. but the example you sent me helped me a lot.
>   I took the code that I adapted to this and what I wanted and resulted in
> perfection.
>    Thank you for your help.
>
>  dataset
>  *with (dataset, plot (dataset $ Na, K $ dataset, $ col = dataset Drug, type
> = "p", pch = 20))
>  with # (dataset, legend ("top", legend = unique (Drug), pch = unique
> (dataset $ Drug)))
>  with (dataset, legend ("bottom", legend = unique (dataset $ Drug), pch =
> 20, col =
>  unique (dataset $ Drug)))*
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Plot-Legend-tp3932687p3934467.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.
>



More information about the R-help mailing list