0

I am plotting a line graph for two groups in Python and I would like to add two trend lines as well. The data is in the data frame, df.

data = [[1, 4.5,'group1'], [2, 4.25,'group1'], [3, 3.75,'group1'],[4, 3.5,'group1'],[1, 4.0,'group2'],[2, 3.75,'group2'],[3, 4.0,'group2'],[4, 4.25,'group2']] 

#Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['xvar', 'yvar','category'])

I use the following code to create a line graph. I would like the trend lines to be dotted instead of the default solid, so I included the linetype='dotted' within geom_smooth(). However, I would like the trend lines to have the respective line colors for the two groups, which I hope to achieve with the scale_color_manual specification.

ggplot(figure1data, aes(x='xvar', y='yvar', color='category')) + geom_line() + geom_point() + labs(x="Xvar",y="Yvar") + ylim(3.5,4.5) + theme_classic() + theme(legend_title=element_blank()) + geom_smooth(mapping=aes(linetype='dotted'),method='lm',se=False) + scale_color_manual(values=('darkblue','darkred'))

But I get this error:

PlotnineError: "Could not evaluate the 'linetype' mapping: 'dotted' (original error: name 'dotted' is not defined)"

I followed the documentation given here. Not sure what is going wrong.

Several resources indicate that linetype can be changed for the two groups but that is not what I want to achieve. I would like to have same line type for the trendlines but one that is different from the main lines.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Rnovice
  • 333
  • 1
  • 5
  • 18
  • Remove the linetype='dotted' from the aesthetic, because it's looking for a column name 'dotted' when it's in the aesthetic. – cookesd Mar 24 '21 at 22:31
  • If I do that, the trend line created by the geom_smooth() command and the main line graph will both be of the same type (for e.g, solid line). But I want a different line type for the trend lines. – Rnovice Mar 29 '21 at 02:27

1 Answers1

0

This could be achieved by setting the linetype as an argument outside of aes() like so:

import pandas as pd
from plotnine import *

data = [[1, 4.5,'group1'], [2, 4.25,'group1'], [3, 3.75,'group1'],[4, 3.5,'group1'],[1, 4.0,'group2'],[2, 3.75,'group2'],[3, 4.0,'group2'],[4, 4.25,'group2']] 

#Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['xvar', 'yvar','category'])

(ggplot(df, aes(x='xvar', y='yvar', color='category')) + 
  geom_line() + 
  geom_point() + 
  labs(x="Xvar",y="Yvar") + 
  ylim(3.5,4.5) + 
  theme_classic() + 
  theme(legend_title=element_blank()) + 
  geom_smooth(linetype='dotted', method='lm',se=False) + 
  scale_color_manual(values=('darkblue','darkred')))

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51