has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
4k stars 213 forks source link

Overlapping labels and wrong direction of cols in geom_col #741

Closed luifrancgom closed 8 months ago

luifrancgom commented 8 months ago

Description

There is a problem in relation to the labels and the direction of the columns when using geom_col

Steps to reproduce and actual behavior

import pandas as pd
from plotnine import (
      ggplot, aes, 
      geom_col
)
from plotnine.data import mtcars

df = (mtcars.
      loc[:, ["name", "disp"]])

(ggplot(data=df,
        mapping=aes(x="disp", y="name")) +
 geom_col())
#> <Figure Size: (640 x 480)>

Expected behavior

I was expecting a similar result as in the case of R:

library(ggplot2)
library(tibble)

tbl <- mtcars |> 
  rownames_to_column(var = "name") |>
  _[c("disp", "name")]

ggplot(data=tbl,
       mapping=aes(x=disp, y=name)) +
    geom_col()

Created on 2024-01-12 with reprex v2.0.2

Session information

Session info --------------------------------------------------------------------
Platform: Windows-10-10.0.22621-SP0 (64-bit)
Python: 3.11
Date: 2024-01-12
Packages ------------------------------------------------------------------------
plotnine==0.12.4
reprexpy==0.3.3
has2k1 commented 8 months ago

Plotnine does not yet automatically detect the orientation of the plot based on the mappings. For now you have to map the discrete variable to the x aesthetic and then use coord_flip.

(ggplot(data=df, mapping=aes(x="name", y="disp"))
 + geom_col()
 + coord_flip()
)
luifrancgom commented 8 months ago

Thank you @has2k1. In this case what you recommend in relation to this issue. Should I close it or you will label it as a possible enhancement in a distant future in relation to detecting the orientation? Please let me know.

Also can you include this solution so I can accept your answer in relation to this post in stackoverflow: https://stackoverflow.com/questions/77656015/problem-using-the-geom-col-in-plotnine-with-linux-ubuntu-22-04-3-lts-and-win?