[R] 2d plotting and colours

Mulholland, Tom Tom.Mulholland at dpi.wa.gov.au
Thu Mar 31 04:14:13 CEST 2005


Since I was only concentrating on colour issues and not on your specific problem I was just showing the possibilities.

Does this code help

n <- 5
par(mfrow = c(2,2))
palette("default")
barplot(1:25,col = 1:25)
palette(rainbow(n))
barplot(1:25,col = 1:25)
palette(rgb((0:15)/15, g=0,b=0, names=paste("red",0:15,sep=".")))
barplot(1:25,col = 1:25)


require(cluster)
x <- runif(100) * 8 + 2
cl <- kmeans(x, n)
palette(rainbow(n))
plot(x, col = cl$cluster)
abline(h = cl$centers, lty = 2,col = "grey" )
palette(palette()[order(cl$centers)])
points(x,col = cl$cluster,pch = 20,cex = 0.4)

However you may wish to choose your colours in a way that is different from cl$cluster which is why I changed the palette before plotting the points. You should see that they now are ordered from bottom to top of the last plot.

You could also choose to create a vector with your colours and then use the value in cl$cluster to select the colours

mycols <- rainbow(n)
plot(x,col = mycols[cl$cluster])

Tom

> -----Original Message-----
> From: Asha Jayanthi [mailto:ashajayanthi at hotmail.com]
> Sent: Thursday, 31 March 2005 9:17 AM
> To: r-help at stat.math.ethz.ch
> Subject: RE: [R] 2d plotting and colours
> 
> 
> Thank you very much.
> 
> the code
> plot(x, col = s)
> points(cl$centers, col = s, pch = 8, cex=2)
> 
> does not plot the points according to the group colors. The 
> plots are used 
> to identify the groups by colors
> 
> That could be done by
> 
> plot(x, col = cl$cluster)
> 
> This means that we need to set the default colours , say col 
> = cl$cluster = 
> a set of group numbers say 1...10 should produce 10 distinct 
> colours points 
> grouped by colour.
> 
> how to do this when you have more than 8 group colours to plot
> 
> >From: "Mulholland, Tom" <Tom.Mulholland at dpi.wa.gov.au>
> >To: <r-help at stat.math.ethz.ch>,"Asha Jayanthi" 
> <ashajayanthi at hotmail.com>
> >Subject: RE: [R] 2d plotting and colours
> >Date: Wed, 30 Mar 2005 15:59:46 +0800
> >
> >And getting back to your question about the palette
> >
> >there are a lot of ways to do this
> >
> >assuming you have just started a session
> >
> >palette()
> ># will give
> >#[1] "black"   "red"     "green3"  "blue"    "cyan"
> >#[6] "magenta" "yellow"  "gray"
> >
> >palette(rainbow(24))  # There's also 'heat.colors' & 'topo.colors'
> >palette()
> >
> ># [1] "red"         "#FF4000"     "#FF8000"
> ># [4] "#FFBF00"     "yellow"      "#BFFF00"
> ># [7] "#80FF00"     "#40FF00"     "green"
> >#[10] "#00FF40"     "#00FF80"     "#00FFBF"
> >#[13] "cyan"        "deepskyblue" "#0080FF"
> >#[16] "#0040FF"     "blue"        "#4000FF"
> >#[19] "#8000FF"     "#BF00FF"     "magenta"
> >#[22] "#FF00BF"     "#FF0080"     "#FF0040"
> >
> >palette(rgb((0:15)/15, g=0,b=0, names=paste("red",0:15,sep=".")))
> >palette()
> ># [1] "black"   "#110000" "#220000" "#330000" "#440000"
> ># [6] "#550000" "#660000" "#770000" "#880000" "#990000"
> >#[11] "#AA0000" "#BB0000" "#CC0000" "#DD0000" "red2"
> >#[16] "red"
> >
> >If you are looking to use colours that take account of 
> colour blindness
> >you could try the package dichromat. (I think 2.1 will have 
> some of this 
> >inbuilt)
> >
> >Once you look through the help files associated with some of 
> these options 
> >you
> >will find the way that best suits your method of working.
> >
> >Tom
> >
> >
> >
> >
> >
> >
> > > -----Original Message-----
> > > From: Uwe Ligges [mailto:ligges at statistik.uni-dortmund.de]
> > > Sent: Wednesday, 30 March 2005 3:18 PM
> > > To: TEMPL Matthias
> > > Cc: r-help at stat.math.ethz.ch; Asha Jayanthi
> > > Subject: Re: [R] 2d plotting and colours
> > >
> > >
> > > TEMPL Matthias wrote:
> > >
> > > > Hi!
> > > >
> > > > There are more than 8 colors.
> > >
> > > Yes, e.g. for rgb space there are 16777216, see ?rgb.
> > >
> > > Uwe Ligges
> > >
> > >
> > >
> > > > x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
> > > >                 matrix(rnorm(100, mean = 1, sd = 0.3), 
> ncol = 2))
> > > >
> > > > (cl <- kmeans(x, i, 20))
> > > >
> > > > s <- c("tomato4", "turquoise", "slateblue", "wheat",
> > > "snow", "skyblue",
> > > > "peru", "pink")
> > > > # see at:
> > > > colors()
> > > >
> > > > plot(x, col = s)
> > > >
> > > > points(cl$centers, col = s, pch = 8, cex=2)
> > > >
> > > >
> > > > Best,
> > > > Matthias
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >>Hi!
> > > >>
> > > >>I am new to R just 3 days in it and i apologize if my
> > > >>questions seem very
> > > >>trivial and consumed your valuable time.
> > > >>
> > > >>I am coding in perl and i stumbled upon R regarding 
> plotting good
> > > >>statistical graphs.
> > > >>
> > > >>I tried the kmean clustering for a large matrix ,say > 150 *
> > > >>150 . I tried
> > > >>the example code given in the tutorial to perform 2d plot
> > > >>
> > > >># i ranges from 2 to 10
> > > >>cl <- kmeans(x, i, 20)
> > > >>plot(x, col = cl$cluster)
> > > >>points(cl$centers, col = 1:i )
> > > >>
> > > >>I see that there are only 8 colours defined , namely
> > > >>black,red,green,blue,cyan,magenta,yello,gray.
> > > >>
> > > >>How should i set my colour preferences to obtain my palette
> > > >>of colours? I
> > > >>checked in the totorial which talks about R.colors and
> > > >>palatte , but i
> > > >>failed to understand how to set it.
> > > >>
> > > >>Thank You
> > > >>
> > > >>Asha
> > > >>





More information about the R-help mailing list