When I am using plotnine, I can use mizani.labels to format the axis labels as percent strings. Is there a similar method to formate geom_label values? I cannot use label = ml.percent("rate")
as this will trigger an error.
import polars as pl
from plotnine import *
import mizani.labels as ml
df = pl.DataFrame({
"group": ["A", "B"],
"rate": [.511, .634]
})
plot = (ggplot(df, aes(x = "group", y = "rate", label = "rate")) +
geom_col() +
geom_label() +
scale_y_continuous(labels = ml.percent)
)
plot.show()
I can preformat a rate_label before adding my dataframe to ggplot, but I was hoping to avoid this prework as I am able to avoid it when using ggplot in R.
df = df.with_columns(rate_label = (pl.col("rate") * 100).round(1).cast(pl.Utf8) + '%')
plot = (ggplot(df, aes(x = "group", y = "rate", label = "rate_label")) +
geom_col() +
geom_label() +
scale_y_continuous(labels = ml.percent)
)
plot.show()
library(tidyverse)
library(scales)
df = tibble(
group = c("A", "B"),
rate = c(.511, .634)
)
ggplot(df, aes(x = group, y = rate, label = percent(rate))) +
geom_col() +
geom_label() +
scale_y_continuous(labels = percent)
Yes you can do it the "same" as in R.
import polars as pl
from plotnine import *
import mizani.labels as ml
df = pl.DataFrame({
"group": ["A", "B"],
"rate": [.511, .634]
})
(
ggplot(df, aes(x="group", y="rate", label="ml.percent(rate)"))
+ geom_col()
+ geom_label()
+ scale_y_continuous(labels=ml.percent)
)