Kathryn B Walters-Conte
2012-Jul-02 20:31 UTC
[R] Code scatter plot data from matrix with 3rd column
Hello I am looking for a simple way to plot my data from a matrix (or data frame) using a 3rd column as category to code the data points. for example: xyz 543240 104230 15901 203241 25781 3042340 357891 405670 45780 50531 Ideally, I'd like 0 or 1 to correspond to a color but I'd settle for a symbol at this point. I have tried working with pch but can't get it to work. Thanks Kat [[alternative HTML version deleted]]
Hello,
In order to avoid messing up the data, use dput. See below, in the end.
As for your question, try this:
set.seed(1234)
xyz <- data.frame(x=sample(20, 10), y=sample(20, 10), z=sample(0:1, 10,
TRUE))
# pch=16 --> solid circle; cex=4 --> 4 fold expansion
with(xyz, plot(x, y, col=z+1, pch=16, cex=4)) # color 0 is white
If you have a matrix, not a data.frame, don't use with(),
use xyz[, "x"], etc.
~~~~~~~~~~~~~~~~ The use of dput() ~~~~~~~~~~~~~~~~
dput(xyz)
structure(list(x = c(3L, 12L, 11L, 18L, 14L, 10L, 1L, 4L, 8L,
6L), y = c(14L, 11L, 6L, 16L, 5L, 13L, 17L, 4L, 3L, 12L), z = c(0L,
0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L)), .Names = c("x", "y",
"z"
), row.names = c(NA, -10L), class = "data.frame")
Now all anyone has to do is to copy that output, from 'structure' onward
and paste it in an R session. Try it, assign to a variable:
xyz2 <- structure(...etc...)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Hope this helps,
Rui Barradas
Em 02-07-2012 21:31, Kathryn B Walters-Conte escreveu:> Hello
>
> I am looking for a simple way to plot my data from a matrix (or data frame)
using a 3rd column as category to code the data points.
>
> for example:
> xyz
> 543240
> 104230
> 15901
> 203241
> 25781
> 3042340
> 357891
> 405670
> 45780
> 50531
>
> Ideally, I'd like 0 or 1 to correspond to a color but I'd settle
for a symbol at this point. I have tried working with pch but can't get it
to work.
>
> Thanks
> Kat
> [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> 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.
>
Your data is effectively unreadable. Please use dput() to supply sample data. Here is one way to do what you want for data frames using the ggplot2 package. library(ggplot2) mydata <- data.frame(x = 1:10, y = rnorm(10), z = c(rep(1,4), rep(2, 6))) ggplot(mydata, aes(x, y, colour= as.factor( z))) + geom_point() John Kane Kingston ON Canada> -----Original Message----- > From: kbw1123 at yahoo.com > Sent: Mon, 2 Jul 2012 13:31:36 -0700 (PDT) > To: r-help at r-project.org > Subject: [R] Code scatter plot data from matrix with 3rd column > > Hello > > I am looking for a simple way to plot my data from a matrix (or data > frame) using a 3rd column as category to code the data points. > > for example: > xyz > 543240 > 104230 > 15901 > 203241 > 25781 > 3042340 > 357891 > 405670 > 45780 > 50531 > > Ideally, I'd like 0 or 1 to correspond to a color but I'd settle for a > symbol at this point. I have tried working with pch but can't get it to > work. > > Thanks > Kat > [[alternative HTML version deleted]] > > ______________________________________________ > 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.____________________________________________________________ GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails