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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With