I have an issue similar to but not resolved by the top response to this question. However, I'm using ggplot2 in python through the rpy2 package which introduces additional difficulties.
I have a number of different time series (with variable names) that I want to plot next to a data series (that is constant). I want the data series to be the same color in all of the plots, but do not care what color the other series are in. However, if I allow ggplot2 to automatically assign colors, it does so in an alphabetical order, and the color mapping is not stable depending on whether the series name is before or after 'data' alphabetically. (see code below)
Note that the series names ('a_model', 'e_model' in the code example) are not all known beforehand, so I cannot simply create a manual color scale with all the possible series names. Also, plots might include the data series and more than one other series. I am only interested in keeping the data series a constant color.
from rpy2 import robjects
from rpy2.robjects.lib import grid
from rpy2.robjects.packages import importr
import rpy2.robjects.lib.ggplot2 as ggplot2
from rpy2.robjects import pandas2ri
import pandas as pd
pandas2ri.activate()
###Input data###
plot_data={}
plot_data.update({'a_model':[0.217,0.226,0.238,0.253,0.272,0.278,0.283,0.29,0.296,0.298]})
plot_data.update({'data':[0.255,0.226,0.241,0.19,0.264,0.302,0.291,0.26,0.218,0.221]})
plot_data.update({'mos_since_start':[1,2,3,4,5,6,7,8,9,10]})
###Plotting Function###
def plot(plot_data, filename):
df=pd.DataFrame(in_dict)
fig = pd.melt(df, id_vars=['mos_since_start'])
pp = ggplot2.ggplot(fig) + \
ggplot2.aes_string(x='mos_since_start',
y='value',group='variable',colour='variable', shape = 'variable', linetype = 'variable') +\
ggplot2.geom_line() + ggplot2.geom_point()
robjects.r.ggsave(filename=filename, plot=pp, width =12, height = 8)
###Plots###
plot(plot_data,"./testplot.pdf")
plot_data.update({'e_model':plot_data.pop('a_model')})
plot(plot_data,"./testplot2.pdf")