Skip to content

2d plotting and colours

2 messages · Mulholland, Tom, Asha Jayanthi

#
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
#
Thank you.

mycols <- c("brown","orange","tomato")
plot(x,col = mycols[cl$cluster])

this works. I can define distinct colours and check the graph.

the rest of the examples does not give a wide palette to choose from

Asha