sort boxplot based on 25-75th percentiles

9 visualizaciones (últimos 30 días)
Nikolas Spiliopoulos
Nikolas Spiliopoulos el 27 de Jun. de 2020
Comentada: Nikolas Spiliopoulos el 28 de Jun. de 2020
Hi all,
I have a figure with 3 boxplots in it (different colors each of them)
However, I would like to sort them based on the difference between 25th-75th percentile.
Is it possible to do it?
And if yes can I keep the initial colours after they have been sorted?
thanks a lot
NIkolas

Respuesta aceptada

the cyclist
the cyclist el 27 de Jun. de 2020
Yes, it is possible. The algorithm would be something like
  • Find the percentiles on the data, using the prctile command, before plotting them with boxplot
  • Sort those percentiles, and store the sorting order
  • Use a grouping variable (the second argument to the boxplot command), making sure that the grouping variable values are sorted according to the correct order
I could probably give more specific advice if you upload the data and code that you are using now.
  3 comentarios
the cyclist
the cyclist el 28 de Jun. de 2020
Editada: the cyclist el 28 de Jun. de 2020
I think this does what you want. You want the colors of the box plots to be retained from the original, unsorted boxplot, right?
A=[rand(50,1) 2*rand(50,1) 3.5 *rand(50,1) 1.8*rand(50,1) 0.7*rand(50,1) 0.5*rand(50,1) 4*rand(50,1) 3.1*rand(50,1) 7*rand(50,1)]; %Random matrix
p25 = prctile(A,25);
p75 = prctile(A,75);
[~,sortingOrder] = sort(p75-p25);
[~,unsortingOrder] = sort(sortingOrder);
sortedA = A(:,sortingOrder);
figure
g=boxplot(sortedA);
set(g(5,unsortingOrder(1:3)), 'Color', 'r');
set(g(5,unsortingOrder(4:6)), 'Color', 'b');
set(g(5,unsortingOrder(7:9)), 'Color', 'g');
Nikolas Spiliopoulos
Nikolas Spiliopoulos el 28 de Jun. de 2020
Thanks a lot,
that's exactly what I need
cheers!!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by