Home > Enterprise >  How can I plot 2 LM lines using ggplot?
How can I plot 2 LM lines using ggplot?

Time:10-07

ggplot(output1, aes(Year, SuPDem, color = as.factor(v2x_regime)))  
  geom_line(size = 0.6)  
  scale_color_manual(values = c("orange", "blue"), labels = c("democracies","autocracies")) 
theme_minimal(base_size = 5)

My df is:

v2x_regime Year SuPDem
0 1990 -0.581147
0 1991 -1.581147
0 1992 2.371147
1 1990 -0.581147
1 1991 -0.471147
1 1992 -0.601147

This works perfectly in generating line graphs. However, I would like to create LM for both democracies and autocracies. Since, I do not plot two different columns, but instead I have a binary variable (v2x_regime) where 0 means autocracies, 1 means democracies it seems more complicated. How can I plor LM for both democracies and autocracies?

CodePudding user response:

Would you be looking for something like this? I only added the geom_smooth() function to what you already have and gave it the the method = "lm" argument. I got the two separate LM lines added to your graph.

LM lines on plot lines:

ggplot(output1, aes(Year, SuPDem, color = as.factor(v2x_regime)))  
    geom_line(size = 0.6)  
    scale_color_manual(values = c("orange", "blue"), labels = c("democracies","autocracies")) 
    theme_minimal(base_size = 5) 
    geom_smooth(method = "lm",se=FALSE)

LM lines on plots:

ggplot(output1, aes(Year, SuPDem, color = as.factor(v2x_regime)))  
    geom_point()  
    scale_color_manual(values = c("orange", "blue"), labels = c("democracies","autocracies")) 
    theme_minimal(base_size = 5) 
    geom_smooth(method = "lm",se=FALSE)
  • Related