title: How to create regression lines for a subset of a graphed data in ggplot2?
body: <p>I have a dataset of measurements from different genera. Some of these genera have multiple datapoints, others have only one. I've written the following code to produce a scatterplot of my <code>measurements</code> table containing <code>Measurement_A</code> and <code>Measurement_B</code>.  </p>
  
  <pre><code># Load ggplot2 and ggpmisc
  library(ggplot2)
  library(ggpmisc)
  measurements &lt;- read.csv("measurements.csv")
  # Define colours and shapes for each genus (which I've called A, B, C and D)
  colour_specifications &lt;- c("A"="green", "B"="red", "C"="yellow",  "D"="blue")
  shape_specifications &lt;- c("A"=15, "B"=16, "C"=17, "D"=18)
  
  # Create scatterplot of data from each genus
  # and plot a regression line for each genus and show the R^2 value for each line
  scatterplot &lt;- ggplot(measurements, aes(x=Measurement_A, y=Measurement_B,     
  colour=Genus, shape=Genus))+
  +   geom_point()+theme_classic()+
  +   scale_color_manual(values = colour_specifications)+
  +   scale_shape_manual(values= shape_specifications)+
  +   geom_smooth(method=lm, se=FALSE)+
  +   stat_poly_eq(formula = y ~ x, eq.with.lhs=FALSE, parse = TRUE)
  &gt; scatterplot
  </code></pre>
  
  <p>Despite genera C and D only having one datapoint the legends show the regression line as well as the symbol. I would like to find a way to plot regression lines for just those subset of genera that have multiple datapoints (genera A and B). I have tried the <code>subset</code> function as follows,</p>
  
  <pre><code>scatterplot &lt;- ggplot(measurements, aes(x=Measurement_A, y=Measurement_B,
      colour=Genus, shape=Genus))+
    geom_point()+theme_classic()+
    scale_color_manual(values = colour_specifications)+
    scale_shape_manual(values= shape_specifications)+
    geom_smooth(data=subset(measurements, Genus="A"),
            method='lm', se=FALSE)+
    geom_smooth(method=lm, se=FALSE)+
    stat_poly_eq(formula = y ~ x, eq.with.lhs=FALSE, parse = TRUE)
  </code></pre>
  
  <p>but it hasn't made any difference to the legend which makes me think I've done something wrong. </p>
  
  <p>Is there a way to plot the regression lines for just genera A and B and not C and D? </p>
  
  <p>ETA:</p>
  
  <p>This is the data I'm using for this:</p>
  
  <pre><code>Genus &lt;- c("A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "C",     "D") 
  Specimen &lt;- c("Aa","Ab","Ac","Ad","Ae","Af","Ag","Ah","Ba", "Bb", "Bc", "Bd", "Ca", "Da") 
  Measurement_A &lt;- c(60, 80, 100, 105, 120, 130, 140, 95, 70, 80, 90, 100, 170, 55) 
  Measurement_B &lt;- c(10, 15, 30, 30, 35, 40, 40, 27, 10, 17, 20, 27, 15, 5)             
  measurements &lt;- data.frame(Genus, Specimen, Measurement_A, Measurement_B)
  </code></pre>
  
  <p>All I want to do is create a single plot of my data with a single legend but with genera A and B showing regression lines and the R^2 values. Both sets of code above give me a legend with regression lines through all the symbols, not just A and B. As I explained above, I have tried the <code>subset</code> function but it does not change the legend. When I tried</p>
  
  <pre><code>scatterplot &lt;- ggplot(measurements, aes(x=Measurement_A, y=Measurement_B,     
                                          colour=Genus, shape=Genus))+
  geom_point()+theme_classic()+
  scale_color_manual(values = colour_specifications)+
  scale_shape_manual(values= shape_specifications)+
  geom_smooth(method=lm, se=FALSE)+
  scale_color_manual(values = c("green","red"),limits = c("A","B"))+
  stat_poly_eq(formula = y ~ x, eq.with.lhs=FALSE, parse = TRUE)
  scatterplot
  </code></pre>
  
  <p>I get an error message saying <code>"Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale"</code> and then I get two legends, one with the preset symbols but all in black for all genera, and one with the colours but just circular symbols for genera A and B. </p>
  
  <p><img src="https://i.stack.imgur.com/lxH9K.png" alt="Plot of data"></p>
  
  <p>ETA:
  I have found the answer, I just needed to add <code>show.legend=FALSE</code> in the <code>geom_smooth</code> bracket.</p>