Boxplot is an R function that represents a numeric vector of data that is split in several groups.

By default, groups are given in a factor, ordered by alphabetical order. (it would be A,B,C,D in this exemple).

Sometimes, we need to show groups in a specific order. So it is necessary to reorder the factor, as it is described below.

If you want to use the average value of the groups to reorder them, please visit this other exemple !

If this does not fit your expectation, visit this page that use the same technique !

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#Creating data names=c(rep("A", 20) , rep("B", 20) , rep("C", 20), rep("D", 20)) value=c( sample(2:5, 20 , replace=T) , sample(6:10, 20 , replace=T), sample(1:7, 20 , replace=T), sample(3:10, 20 , replace=T) ) data=data.frame(names,value) #Classic boxplot (A-B-C-D order) boxplot(data$value ~ data$names) # I reorder the groups order : I change the order of the factor data$names data$names=factor(data$names , levels=levels(data$names)[c(1,4,3,2)]) #The plot is now ordered ! boxplot(data$value ~ data$names , col=rgb(0.3,0.5,0.4,0.6) , ylab="value" , xlab="names in desired order") |

*Not really what you are looking for ? Make a new search !*

## Leave a Reply

Be the First to Comment!