[R] Colour clusters in a 2d plot

hadley wickham h.wickham at gmail.com
Mon Jul 7 21:48:40 CEST 2008


On Mon, Jul 7, 2008 at 12:51 PM, Josep Maria Campanera Alsina
<campaxic at gmail.com> wrote:
> R experts,
>
> I have three columns. c1 and c2 are numeric variables whereas c3 are the
> clusters classes (nominal variable, 10 different: cluster1, cluster2,
> cluster3, cluster4, cluster5 ....). I'd like to plot c1 against c2 (easy!)
> in a 2D plot and put different color depending to the cluster class
> automatically regardless the number of clusters.
>
> Could anyone give a hand?
>
> Josep Maria,
>
>
> matrix <- read.table(fileName, header=TRUE, sep=",")
> c1<-matrix[,1]
> c2<-matrix[,2]
> c3<-matrix[,3]
> plot(c1,c2) ????? what else?

ggplot2 will do this automatically (and provide a legend)

install.packages("ggplot2")
qplot(c1, c2, colour = c3)

You can find out more about ggplot2 at http://had.co.nz/ggplot2

Hadley


-- 
http://had.co.nz/



More information about the R-help mailing list