1

I am trying to reorder plot first by am and then by mpg. Attached in the result in R using ggplot2.

I trying to attain the same result using siuba and plotnine. Below is my code so far.

(
mtcars 
    >> arrange(_.am, _.mpg)
    >> mutate(model = fct_reorder(_.model, _.am))
    >> ggplot(aes(y="mpg", x="model", fill='factor(am)'))
    + geom_col()
    + labs(fill = "Automatic/Manual Transmission")
    + coord_flip()
)

example

Jorengarenar
  • 2,705
  • 5
  • 23
  • 60

1 Answers1

2

If I should replicate your plot in R I would make use of dplyr::arrange + forcats::fct_inorder. As siuba does not offer an equivalent to fct_inorder you could achieve your desired result by first arranging in your desired order, adding an index column of row numbers and reordering by this index column:

from plotnine import *
from siuba import _, arrange, mutate
from siuba.dply.forcats import fct_reorder
(
mtcars 
    >> arrange(-_.am, _.mpg)
    >> mutate(model = fct_reorder(_.model, _.reset_index().index))
    >> ggplot(aes(y="mpg", x="model", fill='factor(am)'))
    + geom_col()
    + labs(fill = "Automatic/Manual Transmission")
    + coord_flip()
)

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51