How to Apply the reorder() Function in R Programming (Example Code)
In this R tutorial you’ll learn how to use the reorder function.
Creation of Example Data
data(iris) # Loading example data head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa |
data(iris) # Loading example data head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
Example: Changing the Order of a Boxplot by Median Values Using reorder() Function
my_reorder <- with(iris, # Reordering groups by median reorder(Species, Sepal.Width, median)) my_reorder # [1] setosa setosa setosa setosa setosa setosa # [7] setosa setosa setosa setosa setosa setosa # [13] setosa setosa setosa setosa setosa setosa # [19] setosa setosa setosa setosa setosa setosa # [25] setosa setosa setosa setosa setosa setosa # [31] setosa setosa setosa setosa setosa setosa # [37] setosa setosa setosa setosa setosa setosa # [43] setosa setosa setosa setosa setosa setosa # [49] setosa setosa versicolor versicolor versicolor versicolor # [55] versicolor versicolor versicolor versicolor versicolor versicolor # [61] versicolor versicolor versicolor versicolor versicolor versicolor # [67] versicolor versicolor versicolor versicolor versicolor versicolor # [73] versicolor versicolor versicolor versicolor versicolor versicolor # [79] versicolor versicolor versicolor versicolor versicolor versicolor # [85] versicolor versicolor versicolor versicolor versicolor versicolor # [91] versicolor versicolor versicolor versicolor versicolor versicolor # [97] versicolor versicolor versicolor versicolor virginica virginica # [103] virginica virginica virginica virginica virginica virginica # [109] virginica virginica virginica virginica virginica virginica # [115] virginica virginica virginica virginica virginica virginica # [121] virginica virginica virginica virginica virginica virginica # [127] virginica virginica virginica virginica virginica virginica # [133] virginica virginica virginica virginica virginica virginica # [139] virginica virginica virginica virginica virginica virginica # [145] virginica virginica virginica virginica virginica virginica # attr(,"scores") # setosa versicolor virginica # 3.4 2.8 3.0 # Levels: versicolor virginica setosa |
my_reorder <- with(iris, # Reordering groups by median reorder(Species, Sepal.Width, median)) my_reorder # [1] setosa setosa setosa setosa setosa setosa # [7] setosa setosa setosa setosa setosa setosa # [13] setosa setosa setosa setosa setosa setosa # [19] setosa setosa setosa setosa setosa setosa # [25] setosa setosa setosa setosa setosa setosa # [31] setosa setosa setosa setosa setosa setosa # [37] setosa setosa setosa setosa setosa setosa # [43] setosa setosa setosa setosa setosa setosa # [49] setosa setosa versicolor versicolor versicolor versicolor # [55] versicolor versicolor versicolor versicolor versicolor versicolor # [61] versicolor versicolor versicolor versicolor versicolor versicolor # [67] versicolor versicolor versicolor versicolor versicolor versicolor # [73] versicolor versicolor versicolor versicolor versicolor versicolor # [79] versicolor versicolor versicolor versicolor versicolor versicolor # [85] versicolor versicolor versicolor versicolor versicolor versicolor # [91] versicolor versicolor versicolor versicolor versicolor versicolor # [97] versicolor versicolor versicolor versicolor virginica virginica # [103] virginica virginica virginica virginica virginica virginica # [109] virginica virginica virginica virginica virginica virginica # [115] virginica virginica virginica virginica virginica virginica # [121] virginica virginica virginica virginica virginica virginica # [127] virginica virginica virginica virginica virginica virginica # [133] virginica virginica virginica virginica virginica virginica # [139] virginica virginica virginica virginica virginica virginica # [145] virginica virginica virginica virginica virginica virginica # attr(,"scores") # setosa versicolor virginica # 3.4 2.8 3.0 # Levels: versicolor virginica setosa
boxplot(Sepal.Width ~ Species, iris) # Base R boxplot with default ordering |
boxplot(Sepal.Width ~ Species, iris) # Base R boxplot with default ordering
boxplot(iris$Sepal.Width ~ my_reorder) # Reordering boxplot by median |
boxplot(iris$Sepal.Width ~ my_reorder) # Reordering boxplot by median