[R] factor interaction boxplot ordering by median

Marc Schwartz marc_schwartz at me.com
Sat Sep 5 15:08:15 CEST 2015


> On Sep 5, 2015, at 7:29 AM, Sergio Fonda <sergio.fonda99 at gmail.com> wrote:
> 
> I would to visualize in boxplot a data frame with two factors ordering one
> factor with the median.
> As example,suppose to have the InsectSprays dataframe, where an "operator"
> factor with two levels, op1 and op2, has been added as shown at bottom here.
> How may be generated a boxplot showing boxes for the interaction spray*op,
> ordered according to the operators' count median for every spray ?
> Thanks in advance for any help!
> Sergio
> ________________________________
> Modified InsectSprays dataframe:

<snip>

Hi,

There is actually an example of reordering factor levels by a calculated numeric value using the InsectSprays data frame in ?boxplot using ?reorder. An interaction can be created by using ?interaction.

Given your data above, in a data frame “DF”:

DF <- structure(list(count = c(10L, 7L, 20L, 14L, 14L, 12L, 10L, 23L, 
17L, 20L, 14L, 13L, 11L, 17L, 21L, 11L, 16L, 14L, 17L, 17L, 19L, 
21L, 7L, 13L, 0L, 1L, 7L, 2L, 3L, 1L, 2L, 1L, 3L, 0L, 1L, 4L, 
3L, 5L, 12L, 6L, 4L, 3L, 5L, 5L, 5L, 5L, 2L, 4L, 3L, 5L, 3L, 
5L, 3L, 6L, 1L, 1L, 3L, 2L, 6L, 4L, 11L, 9L, 15L, 22L, 15L, 16L, 
13L, 10L, 26L, 26L, 24L, 13L), spray = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L), .Label = c("A", "B", "C", "D", "E", "F"), class = "factor"), 
    op = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("op1", "op2"), class = "factor")), .Names = c("count", 
"spray", "op"), class = "data.frame", row.names = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", 
"26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", 
"37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", 
"48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", 
"59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", 
"70", "71", "72"))


# Modify the ?boxplot example
bymedian <- with(DF, reorder(interaction(spray, op), count, median))

> bymedian
 [1] A.op1 A.op1 A.op1 A.op1 A.op1 A.op1 A.op2 A.op2 A.op2 A.op2 A.op2
[12] A.op2 B.op1 B.op1 B.op1 B.op1 B.op1 B.op1 B.op2 B.op2 B.op2 B.op2
[23] B.op2 B.op2 C.op1 C.op1 C.op1 C.op1 C.op1 C.op1 C.op2 C.op2 C.op2
[34] C.op2 C.op2 C.op2 D.op1 D.op1 D.op1 D.op1 D.op1 D.op1 D.op2 D.op2
[45] D.op2 D.op2 D.op2 D.op2 E.op1 E.op1 E.op1 E.op1 E.op1 E.op1 E.op2
[56] E.op2 E.op2 E.op2 E.op2 E.op2 F.op1 F.op1 F.op1 F.op1 F.op1 F.op1
[67] F.op2 F.op2 F.op2 F.op2 F.op2 F.op2
attr(,"scores")
A.op1 B.op1 C.op1 D.op1 E.op1 F.op1 A.op2 B.op2 C.op2 D.op2 E.op2 F.op2 
 13.0  15.0   1.5   4.5   4.0  15.0  15.5  17.0   1.5   5.0   2.5  18.5 
12 Levels: C.op1 C.op2 E.op2 E.op1 D.op1 D.op2 A.op1 B.op1 ... F.op2


boxplot(count ~ bymedian, data = DF,
        xlab = "Interaction of spray and op", ylab = "Insect count",
        main = "Modified InsectSprays Data", varwidth = TRUE,
        col = "lightgray")


Regards,

Marc Schwartz



More information about the R-help mailing list