Reputation: 85
I have a question regarding legend in a plotnine plot.
import pandas as pd
import numpy as np
from pandas.api.types import CategoricalDtype
from plotnine import *
from plotnine.data import mpg
%matplotlib inline
c= pd.read_excel("cenpv.xlsx")
c.head()
dodge_text = position_dodge(width=0.9)
(ggplot(c, aes(x='exon', y='mean'))
+ geom_bar(stat='identity', position='dodge', show_legend=False)
+ geom_text(aes(label='percentage'),
position=dodge_text,
size=8, va='bottom', format_string='{}%')
+ geom_hline(aes(yintercept = "Overall mean", color="Overall mean")))
I expected that legend will have just a yellow line with label overall mean. Is it possible to change it?
Upvotes: 3
Views: 4666
Reputation: 46968
We make something that looks like your data:
c = pd.DataFrame({'exon':['CENPV_'+str(i+1) for i in range(5)],
'mean':np.random.poisson(100,5),
'percentage':np.random.randint(low=10,high=100,size=5)})
c['Overall mean'] = c['mean'].mean()
You have overall mean
as a column, so ggplot2 (or plotnine) interprets it as a range of continuous values to plot the colors over.
What you need to do is to provide the mean as an array and the color as a list:
dodge_text = position_dodge(width=0.9)
(ggplot(c, aes(x='exon', y='mean'))
+ geom_bar(stat='identity', position='dodge', show_legend=False)
+ geom_text(aes(label='percentage'),
position=dodge_text,
size=8, va='bottom', format_string='{}%')
+ geom_hline(aes(yintercept = c['mean'].mean(), color=["Overall mean"]))
+ scale_color_manual(values="yellow",name=' ')
)
Upvotes: 2