Removing regression lines with data that have 3 or less points

by Justin SlingerVanland   Last Updated January 04, 2018 19:26 PM

How do you remove certain regression lines in ggplot 2 but keep others using the stat smooth or geom smooth functions?

I am plotting length ~ weight relationships for fish, comparing between lakes, years, species and parasitism.

I am able to plot regression lines for everything, for example Parasitized vs Non Parasitized, however if one group say for example Parasitized only has 2 points, a regression line is still made for it, as with all the others that have 3 or more points.

My question is how do you plot your data to create regression lines for data that has 3 or more points but at the same time doesn't create regression lines for data that has only two points?

I have included the data and a sample graph with the issue:

Lake B2 2016, 9 Spine Sticklebacks, Parasitized vs Non Parasitized

    > str(B2_2016)

Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 8 obs. of 16 variables:

$ Year : num 2016 2016 2016 2016 2016 ...

$ Sample ID : chr "b2-ss-01" "b2-ss-03" "b2-ss-05" "b2-ss-06" ...

$ Species : chr "P. pungitius" "P. pungitius" "P. pungitius" "P. pungitius" ...

$ Total Wt (g) : num 0.0643 0.923 0.0807 0.1435 0.0292 ...

$ Total Length (cm): num 2.4 5.2 2.7 3 1.9 2.3 3.6 5.7

$ Sex : num 0 0 0 0 0 0 1 1

$ Age : chr "-" "3" "-" "-" ...

$ Liver Wt (g) : chr "-" "4.02E-2" "-" "-" ...

$ Gonad Wt (g) : chr "-" "-" "-" "-" ...

$ Condition (K) : num 0.465 0.656 0.41 0.531 0.426 ...

$ HSI : chr "-" "4.3553629469122424" "-" "-" ...

$ GSI : chr "-" "-" "-" "-" ...

$ Parasites : num 0 0 0 0 0 0 1 1

$ P Weight : num NA NA NA NA NA ...

$ Gut Contents : chr "-" "Y" "-" "-" ...

$ S.I. : chr "-" "Y" "-" "-" ...

x and y axes are log10

Thanks!!!



Related Questions


Plotting both a GLM and LM of same data

Updated February 25, 2017 01:26 AM

Is there a ggplot2 version of termplot()?

Updated July 11, 2017 04:26 AM

Scatter plot with ggplot and predict() R

Updated May 21, 2015 01:11 AM

How do I add a regression line to this?

Updated August 12, 2018 06:26 AM