Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- boxplot(cost ~ type)
- ----- -----
- | |
- [ ] |
- | [ ]
- | |
- ----- -----
- A B
- bymedian <- with(InsectSprays, reorder(spray, -count, median))
- boxplot(count ~ bymedian, data = InsectSprays,
- xlab = "Type of spray", ylab = "Insect count",
- main = "InsectSprays data", varwidth = TRUE,
- col = "lightgray")
- > set.seed(42) # fix seed
- > DF <- data.frame(type=sample(LETTERS[1:5], 100, replace=TRUE),
- + cost=rnorm(100))
- >
- > boxplot(cost ~ type, data=DF) # not ordered by median
- >
- > # compute index of ordered 'cost factor' and reassign
- > oind <- order(as.numeric(by(DF$cost, DF$type, median)))
- > DF$type <- ordered(DF$type, levels=levels(DF$type)[oind])
- >
- > boxplot(cost ~ type, data=DF) # now it is ordered by median
- bymedian <- with(InsectSprays, reorder(spray, -count, median, **na.rm = TRUE**)
- boxplot(count ~ bymedian, data = InsectSprays,
- xlab = "Type of spray", ylab = "Insect count",
- main = "InsectSprays data", varwidth = TRUE,
- col = "lightgray")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement