0

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!

IRTFM
  • 258,963
  • 21
  • 364
  • 487
patrick
  • 4,455
  • 6
  • 44
  • 61
  • 1
    All `factor` class objects have an order for their levels - the only reason to use `ordered()` rather than just `factor()` is for getting a different set of contrasts in a model matrix. – Gregor Thomas Mar 09 '17 at 18:59
  • 2
    I understand that you can't show/share your *real* data, but it would help a lot if you could make your problem reproducible by sharing some small *illustrative* data. Currently, we can't run any of your code, and you seem to say that `df[['category']]` gives output that looks good, and then you run `text()` and the `df[['category']]` output changes - which makes no sense. [Please try to make your example reproducible](http://stackoverflow.com/q/5963269/903061). – Gregor Thomas Mar 09 '17 at 19:02
  • @Gregor thanks for your answers. I'll try to to come up with an example data set. I do feel, however, that at least one of my specific questiond ("what is the output with `3 4 2 1` telling me") was more general and might work w/out a reproducible example. Do you already have any insight on this? – patrick Mar 09 '17 at 19:13
  • That's telling you that "xw" is the third item in your levels specification for your factor assignment, 4 is with "xm", 2 is with "nm" and 1 is with "nm". Factors are actually integer vectors with translation into a `levels`-vector for display.. – IRTFM Mar 09 '17 at 19:17
  • Agree with 42-, but it's surprising because factors usually print their levels like you show above. Seems like you converted it to numeric at some point... – Gregor Thomas Mar 09 '17 at 19:18
  • Agree with Gregor that the integers would not normally have been display ed as values after the factor assignment. I think the puzzlement was a joint lack of understanding about factor-construction as well an how to index color vectors. The vectorized coloring mechanism within R was one of those intellectual hurdles I remember making as I moved along in R that I had not acquired in my earlier programming efforts. – IRTFM Mar 09 '17 at 19:21
  • Thanks, that clarifies a lot for me! – patrick Mar 09 '17 at 19:28

1 Answers1

2

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".

IRTFM
  • 258,963
  • 21
  • 364
  • 487