Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

color points on plot depending of a value of a column

Tags:

plot

r

I have a dataset with 2 columns. With plot(ds$values) I get all the points in black.

Now consider that I have one second column that gives me categories as strings. Notice that I don't know the categories names. How do I plot each category with a different colour.

A simple: plot(ds$values, col=someFn(ds$categories))?

Adding an index would be good but not required.

like image 899
oleber Avatar asked Apr 24 '14 14:04

oleber


1 Answers

The key here is to transform your categorical variable from character to factor. You can simply wrap it in factor as you pass it to the col argument in plot. Here's a simple example:

set.seed(1)
plot(sample(1:10,20,TRUE), col=factor(sample(letters[1:3],20,TRUE)))

Of course, you may want to pick a different color palette than the default.

enter image description here

like image 114
Thomas Avatar answered Sep 28 '22 04:09

Thomas