plotnine icon indicating copy to clipboard operation
plotnine copied to clipboard

Support for `AsIs` variables

Open chentoast opened this issue 1 month ago • 4 comments

ggplot (at least starting from 3.5.0) improved functionality for specifying AsIs variables; that is, variables who do not participate in the scale systems at all and are interpreted literally.

For example, a column of literal colors can be specified (in R) as:

library(tidyverse)
my_colours <- sample(c("red", "green", "blue"), nrow(mpg), replace = TRUE)

ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = I(my_colours)))

and drawing text at the exact center of the plot (regardless of limits and scales) can be done as

ggplot(mpg, aes(displ, hwy)) +
  geom_point(colour = "grey50") +
  annotate(
    "text", 
    label = "Text in the middle",
    x = I(0.5), y = I(0.5),
    size = 8
  )

Does a similar API exist in plotnine? If not, I would love to have something like this - it would make placing text annotations on figures much, much easier. Thanks!

chentoast avatar Nov 04 '25 16:11 chentoast

the scale_*_identity scales should allow you to do similar with regards to the colors.

I'm less certain about the panel-relative coordinates, I know some theme options take such values, but the annotations?

TyberiusPrime avatar Nov 05 '25 08:11 TyberiusPrime

I saw this change, but I'm not sure whether we can do it in plotnine without quirky edge cases. I'll give it a shot for v0.16.0.

has2k1 avatar Nov 05 '25 11:11 has2k1

Related: #939

has2k1 avatar Nov 05 '25 11:11 has2k1

scale_*_identity does do what I want, but 1. There is no scale_{x,y}_identity, and 2. Using AsIs variables allows you to hae both literally interpreted and scaled variables in the plot - whereas using scale_*_identity forces you into only having literal variables along that scale.

Thanks for considering this feature; it's understandable if something like this is too hard to implement.

chentoast avatar Nov 05 '25 15:11 chentoast