title: Finding 2 regression equations/slopes and comparing equations/slopes from a scatter plot
body: <p>I'm using the iris dataset in R. I filtered the dataset so iris$Species == setosa or versicolor. I then created a scatter plot where the x axis is Sepal.Length and the y axis is Sepal.Width. The points were highlighted based on Species, and 2 different linear regression lines were added to the scatter plot based on species.</p>
  <p>Here are my questions:</p>
  <ol>
  <li>Is it possible to get the slope equations/ slope values for the 2 lines (setosa or versicolor) from the scatter plot? If so, how?</li>
  <li>Is it possible to use a statistical test to see if the slope equations/ slope values for the 2 lines (setosa or versicolor) were significantly different from one another?</li>
  </ol>
  <p>Please let me know if/when you can.</p>
  <p>Thanks ahead of time.</p>
  <p>-P.S.</p>
  <p>Here is the figure:</p>
  <p><a href="https://i.stack.imgur.com/i4iJL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i4iJL.png" alt="Scatter plot of Sepal.Length X Sepal.Width with dots as Species where Species is setosa or versicolor" /></a></p>
  <p>Here is the R code to generate the plot:</p>
  <pre><code>
  # creates data for scatter plot
  
  ## dataset of interest
  iris
  
  ## for iris
  colnames (iris)
  
  ### creates dataset with just cases where iris$Species == setosa or versicolor
  
  #### unique values for iris$Species
  unique(iris$Species)
  
  #### loads tidyverse package
  library(tidyverse)
  
  ##### filters dataset with just cases where iris$Species == setosa or versicolor
  iris__setosa_or_versicolor &lt;- iris %&gt;% filter(iris$Species != &quot;virginica&quot;)
  
  ##### turns iris__setosa_or_versicolor to dataframe
  iris__setosa_or_versicolor &lt;- data.frame(iris__setosa_or_versicolor)
  
  ##### unique values for iris__setosa_or_versicolor$Species
  unique(iris__setosa_or_versicolor$Species)
  
  ## creates scatter plot
  
  ### loads ggplot2
  library(ggplot2)
  
  ### Basic scatter plot
  scatter_plot__sepal_length_x_sepal_width__points_is_species &lt;- ggplot(iris__setosa_or_versicolor, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point()
  scatter_plot__sepal_length_x_sepal_width__points_is_species
  
  ### Basic scatter plot with regression line added
  scatter_plot__sepal_length_x_sepal_width__points_is_species &lt;- ggplot(iris__setosa_or_versicolor, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point() + geom_smooth(method=lm, se=FALSE, color=&quot;green&quot;)
  scatter_plot__sepal_length_x_sepal_width__points_is_species
  
  ### Basic scatter plot separated by Species
  scatter_plot__sepal_length_x_sepal_width__points_is_species &lt;- ggplot(iris__setosa_or_versicolor, aes(x=Sepal.Length, y=Sepal.Width, color=Species, shape=Species)) + geom_point() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE) + labs(title=&quot;Scatter plot of Sepal.Length X Sepal.Width with dots as Species where Species is setosa or versicolor&quot;, x=&quot;Sepal.Length&quot;, y = &quot;Sepal.Width&quot;) + scale_colour_manual(values = c(&quot;#ff0000&quot;,&quot;#0000ff&quot;))
  scatter_plot__sepal_length_x_sepal_width__points_is_species
  
  scatter_plot__sepal_length_x_sepal_width__points_is_species &lt;- 
    scatter_plot__sepal_length_x_sepal_width__points_is_species + theme(panel.background = element_rect(fill = &quot;white&quot;, colour = &quot;white&quot;, size = 0.5, linetype = &quot;solid&quot;), panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = &quot;lightblue&quot;), panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = &quot;lightblue&quot;))
  scatter_plot__sepal_length_x_sepal_width__points_is_species
  
  scatter_plot__sepal_length_x_sepal_width__points_is_species &lt;- 
    scatter_plot__sepal_length_x_sepal_width__points_is_species + geom_point(size=3)
  scatter_plot__sepal_length_x_sepal_width__points_is_species
  
  ### displays scatter plot
  scatter_plot__sepal_length_x_sepal_width__points_is_species
  
  </code></pre>
  <p>EDIT 1:</p>
  <p>Response to comment:</p>
  <blockquote>
  <p>What do you mean in 2.? Do you want to also add the result of the test as an annotation on the figure? Or just compare the slopes independently of the figure? Please, edit your question. I will answer once this is clear. (As a general comment, try to avoid including in the example code snippet details that are irrelevant to your question, such as change in background color and size of points.)</p>
  </blockquote>
  <p>I'm interested in comparing the slopes independent of the figure. I want to see if there are differences between the regression lines and how to interpret those differences.</p>
  <p>Response to answer:</p>
  <blockquote>
  <p>Run a regression using lm.</p>
  <p>Then use ANCOVA on those regressions to see a slope difference.</p>
  </blockquote>
  <p>Thanks. I think I've tried to do what you said. The anova table comparing the model with v. without the interaction was significant. I think this means that there is a difference between the slopes of the regression based on the grouping variable species. Is this interpretation correct?</p>
  <p>The code is below. was the code done properly?</p>
  <p>Follow up question for this: How do I find the slopes of the 2 regression lines (iris$Species = setosa v. versicolor) based on the figures?</p>
  <p>Here is the code with ANCOVA comparing the 2 regressions:</p>
  <pre><code>
  ## comparing the slopes of the regression lines using ANCOVA
  # ---- NOTE: DV - Sepal.Width
  # ---- NOTE: IV - Sepal.Length
  # ---- NOTE: grouping variable: Species
  # ---- NOTE: dataset: iris__setosa_or_versicolor
  # ---- NOTE: based on this site: https://stats.stackexchange.com/questions/51780/how-to-perform-an-ancova-in-r
  
  ### create interaction_regression_model
  interaction_regression_model &lt;- aov(Sepal.Width~Sepal.Length*Species,data=iris__setosa_or_versicolor)
  
  #### gives summary of interaction_regression_model
  summary(interaction_regression_model)
  
  ### create no_interaction_regression_model
  no_interaction_regression_model &lt;- aov(Sepal.Width~Sepal.Length+Species,data=iris__setosa_or_versicolor)
  
  #### gives summary of no_interaction_regression_model
  summary(no_interaction_regression_model)
  
  ### compare 2 regression models, using ancova through anova command
  anova(no_interaction_regression_model,interaction_regression_model)
  
  </code></pre>