[R] plotting points in random but different colors based on condition

Juliet Hannah juliet.hannah at gmail.com
Thu Aug 6 01:49:45 CEST 2009

Maybe this is helpful.

Install ggplot2.

#Create a small example
x <- seq(1:20)
y <- (2*x) + rnorm(length(x),0,1)
id <- rep(1:5,each=4)
dat <- data.frame(x,y,id)

p <- ggplot(dat,aes(x=x,y=y,colour=factor(id)))
p <- p + geom_point()

If this is not the correct structure, maybe including an example will
help. For example,
you can output your data or a subset with dput.

structure(list(x = 1:20, y = c(3.52519254181276, 5.24044281644567,
7.47776852203483, 7.4451075539517, 9.85463703204831, 12.533885478775,
14.2364347537978, 15.6031588004515, 17.2400819084948, 20.2053499115504,
21.4994848677570, 23.6354345765166, 24.5523452698087, 27.3185637999515,
28.5812163905758, 32.6805764070542, 37.0894956595204, 35.0009332656281,
39.6077996102053, 41.0996392969496), id = c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L)), .Names = c("x",
"y", "id"), row.names = c(NA, -20L), class = "data.frame")



On Wed, Aug 5, 2009 at 7:00 PM, per freem<perfreem at gmail.com> wrote:
> hi all,
> suppose I have a file with several columns, and I want to plot all points
> that meet a certain condition (i.e. all points in columns A and B that have
> a value greater than such and such in column C of the data) in a new but
> random color.  That is, I want all these points to be colored differently
> but I dont care what color.  The only concern is that the points will be
> colored as differently from each other as possible.
> The specific example I have is a file with three columns, X, Y and ID.  I
> want to plot all rows from X, Y (i.e. all points) that have the same value
> (say 1) in their ID column as one color, all points from X, Y that have the
> same ID column value (say 2) as a different color, etc.  I dont know ahead
> of time how many values the ID column will have so I can't write a separate
> plot statement for each of these sets of X, Y rows that have the same ID
> value.
> Is there a way to express this in R?
> thank you.
> Is there a way to do this?
>        [[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.

More information about the R-help mailing list