Reputation: 4852
I'm trying to plot some data with ordered factors in R, more specifically trying to plot several data points per factor. For better readability, I'm trying to plot the names with text
, then color them for better readability.
I set it up like so:
df[['category']]=ordered(df[['category']], levels=c("nm", "nw", "xw", "xm"))
Which gives me this output, which looks good to me:
[1] xw xm nw nm
Levels: nm < nw < xw < xm
I plot it like this:
text(xval, temp, label= df[['category']], col=seq(1,4))
(Where xval and temp vary depending on input; the seq in col
is supposed to color it). But then my plot does not look right (xw takes the place of nm).
Printing the output of df[['category']]
gives me this:
3 4 2 1
And I don't know how to read that; what does this mean?
Is this the way it needs to look or does this tell me what's wrong with my plot?
I'm sorry I can't show my plots here but this is research with data that are not mine. Any help is much appreciated!
Upvotes: 0
Views: 542
Reputation: 263421
My guess is that you actually want:
text(xval, temp, label= df[['category']], col=palette()[ df[['category']] ] )
You appear to have been hoping to get the standard palette colors associated with the (ordered) levels of your factor variable. As pointed out by @Gregor, you do not need to use ordered()
for this because the ordering is established by your levels argument. You could substitute a different color vector for the palette()
, for instance using c("red","green", "blue", "orange")
would have resulted in the "xm"
-level text being displayed as "orange".
Upvotes: 2