How to define groups in `plotnine` using interactions of features

I cannot find how to do the equivalent of ggplot’s interaction feature.

What I’d like to do

This site has an example that has the sort of thing I am looking to reproduce: I have two discrete factors (in the example, store and promotion), and I would like to produce a line plot with each unique combination making a “group” to connect the lines with.

Example in ggplot

Taking it from the example above:

#create line plot with values grouped by store and promo
ggplot(df, aes(x=week, y=sales, color=store, shape=promo,
               group=interaction(store, promo))) + 
  geom_point(size=3) +
  geom_line()

What I’d expect to work in plotnine

import plotnine as p9
import pandas as pd

df = pd.DataFrame({
    'week': [1,2,3,4]*4,
    'store': (["A"]*8 + ["B"]*8),
    'promo': (["promo1"]*4 + ["promo2"]*4)*2,
    'sales': [1, 2, 6, 7, 2, 3, 5, 6, 3, 4, 7, 8, 3, 5, 8, 9]
})

#create line plot with values grouped by store and promo
(
  p9.ggplot(df, p9.aes(x="week", y="sales", color="store", shape="promo",
               group="interaction(store, promo)")) + 
  p9.geom_point(size=3) +
  p9.geom_line()
)

This breaks because it doesn’t know what interaction is. If I want the plot, I have to create a column in the data solely for defininte groups:

# ... as before

df['group_col'] = df.apply(lambda row: (row.store, row.promo), axis=1)
(
  p9.ggplot(df, p9.aes(x="week", y="sales", color="store", shape="promo",
               group="group_col")) + 
  p9.geom_point(size=3) +
  p9.geom_line()
)

This isn’t great, because it means I am manually creating (and the tidying columns) for every plot grouping that makes sense, rather than the R-style approach where the interaction happens inside p9 but doesn’t alter the actual data frame.

Addendum for interaction comment

An answer below stated I wanted to use interaction which was a R feature, and wasn’t in Python (and thus wasn’t part of a plotnine question). That wasn’t what was being asked for; in the expectation section I was expecting to use interaction in a similar way I use factor.

In more detail: The expectation part didn’t expect to use interaction as a function, it was part of string interpolation:

       .... group="interaction(store, promo)"))

The python interpreter never sees a call to interaction, it only sees a string. This is similar to how factor works in plotnine — factor is a base R function, but we can use

       .... color="factor(numeric_id)"

in a plotnine geom, and it will interpret the numeric_id as a discrete variable, rather than a continuous one.

As week is a numeric adding a group aes isn’t necessary. For this reason I slightly modified the example data by making week a character so that we end up with an example where both ggplot2 and plotnine require to explicitly map on the group aes to get a line .

Based on the modified example a possible workaround or an alternative to the use of interaction which I use quite often in ggplot2 is to concatenate the columns, i.e. in R I would do group = paste(store, promo, sep = ".") and in plotnine we can do group="store + '.' + promo" (Actually adding a separator is not required but I made it a habit to add it to resemble the output of interaction).

import plotnine as p9
import pandas as pd

df = pd.DataFrame({
    'week': ['1', '2', '3', '4']*4,
    'store': (["A"]*8 + ["B"]*8),
    'promo': (["promo1"]*4 + ["promo2"]*4)*2,
    'sales': [1, 2, 6, 7, 2, 3, 5, 6, 3, 4, 7, 8, 3, 5, 8, 9]
})

#create line plot with values grouped by store and promo
(
  p9.ggplot(df, p9.aes(x="week", y="sales", color="store", shape="promo",
            group="store + '.' + promo")) + 
  p9.geom_point(size=3) +
  p9.geom_line()
)

Recognized by R Language Collective

Sorry for the verbosity of this response, but I think there’s two separate questions here.

Create the same plot w/ plotnine

Unless I’m missing something, I don’t think adding a group to aesthetics added anything to the plot.

Using ggplot2

Below is the R code you referenced:

library(ggplot2)
df <- data.frame(store=rep(c('A', 'B'), each=8),
                 promo=rep(c('Promo 1', 'Promo 2'), each=4, times=2),
                 week=rep(c(1:4), times=4),
                 sales=c(1, 2, 6, 7, 2, 3, 5, 6, 3, 4, 7, 8, 3, 5, 8, 9))

# With Interaction
ggplot(df, aes(x=week, y=sales, color=store, shape=promo,
               group=interaction(store, promo))) + 
  geom_point(size=3) +
  geom_line()


# Without Interaction
ggplot(df, aes(x=week, y=sales, color=store, shape=promo)) + 
  geom_point(size=3) +
  geom_line()

Created on 2024-09-11 with reprex v2.1.1

Using plotnine

Just remove the group from your plotnine call:

import plotnine as p9
import pandas as pd

df = pd.DataFrame({
    'week': [1,2,3,4]*4,
    'store': (["A"]*8 + ["B"]*8),
    'promo': (["promo1"]*4 + ["promo2"]*4)*2,
    'sales': [1, 2, 6, 7, 2, 3, 5, 6, 3, 4, 7, 8, 3, 5, 8, 9]
})

#create line plot with values grouped by store and promo
(
  p9.ggplot(df, p9.aes(x="week", y="sales", color="store", shape="promo")) + 
  p9.geom_point(size=3) +
  p9.geom_line()
)

Using interaction() in python

You say interaction doesn’t work in plotnine.interaction() is a function in base R. It’s not a part of ggplot2. You’re calling it in a python environment, but it’s not a function in python that you can reference.

If you want to create interaction terms in python, here’s a useful reference for doing so.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật