Skip to content

Plot Legend

4 messages · RMSOPS, R. Michael Weylandt

#
Good Afternoon,


I am inexperienced in data visualization R. so I wonder if someone can help
me.
I am using the generic database mtcars.
I would like to change the chart plot, instead of appearing the name of the
medicine, I wanted a symbol in the chart drugX arise for example in a yellow
circle, and so for the rest of the drugs.

I am using the following code.



dataset
    Age Sex     BP Cholesterol       Na        K  Drug
1    23   F   HIGH        HIGH 0.792535 0.031258 drugY
2    47   M    LOW        HIGH 0.739309 0.056468 drugC
3    47   M    LOW        HIGH 0.697269 0.068944 drugC
4    28   F NORMAL        HIGH 0.563682 0.072289 drugX
5    61   F    LOW        HIGH 0.559294 0.030998 drugY
6    22   F NORMAL        HIGH 0.676901 0.078647 drugX
7    49   F NORMAL        HIGH 0.789637 0.048518 drugY
8    41   M    LOW        HIGH 0.766635 0.069461 drugC
9    60   M NORMAL        HIGH 0.777205 0.051230 drugY
10   43   M    LOW      NORMAL 0.526102 0.027164 drugY
11   47   F    LOW        HIGH 0.896056 0.076147 drugC

plot(dataset$Na, dataset$K, main="Na vs K", 
   xlab="Na", ylab="K", pch=20, col="blue")
 text(dataset$Na,dataset$K,dataset$Drug, cex=0.6, pos=4, col="red")


The idea is that instead of writing the name had to be a legend and symbol
was designed in a different color for each drug

Thanks


--
View this message in context: http://r.789695.n4.nabble.com/Plot-Legend-tp3932687p3932687.html
Sent from the R help mailing list archive at Nabble.com.
#
Are you sure you are using the data set mtcars? That data set doesn't
have variables Na or K nor does it look like what you provided...

For the real mtcars, you could try this which I think does something
like what you want.

with(mtcars, plot(cyl, mpg, pch = carb, col = gear))
with(mtcars, legend("top", legend = unique(carb), pch = unique(carb)))
with(mtcars, legend("bottom", legend = unique(gear), pch = 20, col =
unique(gear)))


Michael
On Mon, Oct 24, 2011 at 6:21 AM, 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.
#
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: