1

I want to add two linear fits to my plot (Yes in ggplo2). The first fit will use all my points, the second will exclude a point (let's say first point).

dosis <- c(0.24, 0.33, 0.26, 0.18, 0.11, 0.05)
corriente <-c(301.3, 275.4, 253.8, 235.5, 219.8, 205.8)
library(ggplot2)
datos <-cbind.data.frame(dosis, corriente)
ggplot(datos, aes(x=corriente, y=dosis)) + geom_point() + geom_smooth(method=lm, se=FALSE)

This will give me my plot with my first fit (using all my points).. Now How do I exclude a datum and create the second fit? Do I need to add another geom_smooth command?

Thanks

Parfait
  • 104,375
  • 17
  • 94
  • 125
murpholinox
  • 639
  • 1
  • 4
  • 14

1 Answers1

4

You can add another geom_smooth(), and specify a subset of your dataframe to plot in the second geom_smooth() call. Here, I've excluded your highest value for the corriente variable.

ggplot(datos, aes(x=corriente, y=dosis)) + 
  geom_point() + 
  geom_smooth(method=lm, se=FALSE) +
  geom_smooth(method = lm, se = FALSE, #add a second geom_smooth line 
               #use only a subset of your dataframe
              data = datos[-1,])

enter image description here

Jan Boyer
  • 1,540
  • 2
  • 14
  • 22
  • 2
    Comparisons like `==` or `!=` should not be applied to [floating point values](https://stackoverflow.com/q/9508518/). To exclude a point from the dataset it would be better to use the index. For instance, to remove the first data point in this set one could use `data = datos[-1,]` instead of `data = datos[datos$corriente != 301.3,]`. Alternatively, more lengthy commands like `!isTRUE(all.equal())` would be needed. – RHertel Jul 06 '18 at 06:43
  • 2
    Thanks for the heads up @RHertel, I didn't know about the issue with floating point values. I've edited my answer per your suggestion so people who see this question in the future subset the data correctly. – Jan Boyer Jul 06 '18 at 15:47