Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does reorder function work?

Tags:

r

Let

vetA <- structure(
  c(1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 2L), 
  .Label = c("Two", "One", "Three"), 
  class = "factor"
)
vetB <- reorder(vetA, c(9,1,2,3,4,8,7,6,10,5))
plot(vetA)
plot(vetB)

The plots show differently.

plot(VetA)

enter image description here

plot(VetB)

enter image description here

However, reading the ?reorder manual, I didn't understand how the function works and how to manipulated it, so I could display the bars in this order One Two Three. Also, I have read this post in which uses reorder. But, it got me more confused because he used as a second argument a vector not with index, but with percent.

So, could you please have some mercy & give me a hand?

like image 544
theBotelho Avatar asked Jan 25 '26 23:01

theBotelho


1 Answers

Use factor and specify in which order the levels are. You can also mark the factor as an ordered factor.

vetC <- factor(vetA, levels = c("One", "Two","Three"))
plot(vetC)
like image 77
Roman Luštrik Avatar answered Jan 27 '26 13:01

Roman Luštrik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!