Home > Software engineering >  Put two linear regression lines into one plot
Put two linear regression lines into one plot

Time:10-17

I initially have two data frame("icelandma,swissfe") with observations from different countries and totally same variables. To compare the linear regression of intp.trust and confidence for these two countries. I combine these two data frame into one with this command:

merge1 <- rbind(icelandma, swissfe)

And then, I draw the linear regression plot with this command:

ggplot(data = merge1,aes(x=intp.trust,y=confidence)) 
      geom_point(size=0.5) 
      geom_smooth(method = "lm",formula = y~x) 
      facet_grid(countryname~.)

The plot is like thisenter image description here

The regression line is still in two plots, I'm wondering if there is any way to post these two lines in the same plot. Thanks for your help in advance!

CodePudding user response:

Try

ggplot(data = merge1,aes(x=intp.trust,y=confidence, group = countryname)) 
      geom_point(size=0.5) 
      geom_smooth(method = "lm",formula = y~x)

facet_wrap puts your plots in different panels by countryname~.

If you want to differentiate by countryname add color to your aes: aes(...,color = countryname).

  •  Tags:  
  • r
  • Related