I’d like to sort a categorical variable my own way. I have grouped my dataset into categories like “1-5″,”6-10″,”11-20” …. “>251” and so forth. If plot the variables or display them in a table the sequence of the legend respectively the sequence in the label is “messed up”.
This is not surprising since R does not know that these unordered variables are ordered in fact. Is there a way to attach a manually defined sequence to them?
thx for any suggestions in advance!
Categorical variables are stored as (or converted to be)
factors when you plot them. The order they appear in the plot depends upon the levels of the factor.You likely want to use
cutto create your groups. e.g.This problem is also very similar to another recent SO question.