Reputation: 1109
I want to change the facet title in plotnine (python ggplot) when 2 variables are used in faceting WITHOUT changing the data itself.
Example: using mpg data
pretty = p9.ggplot(data=mpg, mapping = p9.aes(x='hwy', y='displ'))+ p9.geom_point(alpha = 0.6)
pretty + p9.facet_grid("cyl~year")
I have the following plot:
I want to change the year to "yr 1999" and "yr 2008" and the cly titles to "c4", "c5","c6", "c8". But when I used the labeller in facet_grid:
pretty + p9.facet_grid("cyl~year", labeller = ["c4", "c5","c6", "c8", "yr 1999", "yr 2008"])
the plot is not changed. I have tried to google around but did not find answer for changing faceting titles with two variables.
Upvotes: 2
Views: 2189
Reputation: 2365
See the documentation for the labeller
function.
import plotnine as p9
from plotnine.data import mpg
def col_func(s):
return 'yr ' + s
pretty = p9.ggplot(data=mpg, mapping = p9.aes(x='hwy', y='displ'))+ p9.geom_point(alpha = 0.6)
pretty + p9.facet_grid("cyl~year", labeller=p9.labeller(cols=col_func))
Upvotes: 2