R - ordering in boxplot

前端 未结 2 1184
刺人心
刺人心 2020-11-29 07:08

I am trying to produce a series of box plots in R that is grouped by 2 factors. I\'ve managed to make the plot, but I cannot get the boxes to order in the c

相关标签:
2条回答
  • 2020-11-29 07:46

    This earlier StackOverflow question shows how to reorder a boxplot based on a numerical value; what you need here is probably just a switch from factor to the related type ordered. But it is hard say as we do not have your data and you didn't provide a reproducible example.

    Edit Using the dataset you posted in variable md and relying on the solution I pointed to earlier, we get

    R> md$Species <- ordered(md$Species, levels=c("G", "R", "B"))
    R> md$Treatment <- ordered(md$Treatment, levels=c("L", "M", "H"))
    R> with(md, boxplot(Nitrogen ~ Species * Treatment))
    

    which creates the chart you were looking to create.

    This is also equivalent to the other solution presented here.

    0 讨论(0)
  • 2020-11-29 08:05

    The following commands will create the ordering you need by rebuilding the Treatment and Species factors, with explicit manual ordering of the levels:

    mydata$Treatment = factor(mydata$Treatment,c("L","M","H"))
    
    mydata$Species = factor(mydata$Species,c("G","R","B"))
    

    alt text


    edit 1 : oops I had set it to HML instead of LMH. fixing.

    edit 2 : what factor(X,Y) does:

    If you run factor(X,Y) on an existing factor, it uses the ordering of the values in Y to enumerate the values present in the factor X. Here's some examples with your data.

    > mydata$Treatment
    [1] L M H L M H
    Levels: H L M
    > as.integer(mydata$Treatment)
    [1] 2 3 1 2 3 1
    > factor(mydata$Treatment,c("L","M","H"))
    [1] L M H L M H                               <-- not changed
    Levels: L M H                                 <-- changed
    > as.integer(factor(mydata$Treatment,c("L","M","H")))
    [1] 1 2 3 1 2 3                               <-- changed
    

    It does NOT change what the factor looks like at first glance, but it does change how the data is stored.

    What's important here is that many plot functions will plot the lowest enumeration leftmost, followed by the next, etc.

    If you create factors simply using factor(X) then usually the enumeration is based upon the alphabetical order of the factor levels, (e.g. "H","L","M"). If your labels have a conventional ordering different from alphabetical (i.e. "H","M","L"), this can make your graphs seems strange.

    At first glance, it may seem like the problem is due to the ordering of data in the data frame - i.e. if only we could place all "H" at the top and "L" at the bottom, then it would work. It doesn't. But if you want your labels to appear in the same order as the first occurrence in the data, you can use this form:

     mydata$Treatment = factor(mydata$Treatment, unique(mydata$Treatment))
    
    0 讨论(0)
提交回复
热议问题