2
votes

How do you remove certain regression lines in ggplot2 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

Year = c(2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016), 
`Sample ID` = c("b2-ss-01", "b2-ss-03", "b2-ss-05", "b2-ss-06", "b2-ss-07", "b2-ss-08", "b2-ss-02", "b2-ss-04"), 
Species = c("P. pungitius", "P. pungitius", "P. pungitius", "P. pungitius", "P. pungitius", "P. pungitius", "P. pungitius", "P. pungitius"), 
`Total Wt (g)` = c(0.0643, 0.923, 0.0807, 0.1435, 0.0292, 0.0689, 0.13, 1.1902), 
`Total Length (cm)` =    c(2.4, 5.2, 2.7, 3, 1.9, 2.3, 3.6, 5.7), 
Sex = c(0, 0, 0, 0, 0, 0, 1, 1), 
Age = c("-", "3", "-", "-", "-", "-", "2", "3.3"), 
`Liver Wt (g)` = c("-    ","4.02E-2", "-", "-", "-", "-", "8.9999999999999993E-3", "3.3799999999999997E-2"),                                                            
`Gonad Wt (g)` = c("-", "-", "-", "-", "-", "-", "2.3999999999999998E-3", "4.5999999999999999E-3"),                                               
`Condition (K)` = c(0.465133101851852, 0.656434911242603, 0.409998475842097, 0.531481481481481, 0.425718034698936, 0.56628585518205, 0.27863511659808, 0.642680878866912),                                                           
HSI = c("-", "4.3553629469122424", "-", "-", "-", "-", "6.9230769230769225", "2.8398588472525623"), 
GSI = c("-", "-", "-", "-", "-", "-", "1.846153846153846", "0.38648966560241976"), 
Parasites = c(0, 0, 0, 0, 0, 0, 1, 1), 
`P Weight` = c(NA, NA, NA, NA, NA, NA, 0.1918, 0.0586), 
`Gut Contents` = c("-", "Y", "-", "-", "-", "-", "Y", "N"), 
S.I. = c("-", "Y", "-", "-", "-", "-", "Y", "Y")), 
.Names = c("Year", "Sample ID", "Species", "Total Wt (g)", 
"Total Length (cm)", "Sex", "Age", "Liver Wt (g)", "Gonad Wt (g)", 
"Condition (K)", "HSI", "GSI", "Parasites", "P Weight", "Gut Contents", 
"S.I."), row.names = c(NA, -8L), class = c("tbl_df", "tbl", "data.frame"))

So I am getting these errors...

attach(All_Years_All_Lakes_All_Species)
> SticklesDataF = group_by(All_Years_All_Lakes_All_Species, Species, Year, Parasites, Lake) %>% mutate(n = n()), LogLength = log('Total Length (cm)'), LogWeight = log('Total Wt (g)')) 
Error: unexpected ',' in "SticklesDataF = group_by(All_Years_All_Lakes_All_Species, Species, Year, Parasites, Lake) %>% mutate(n = n()),"
1
Use dplyr or data.table or whatever to flag groups with 3 or less points, subset the data for the geom_smooth based on that flag. - Gregor Thomas
This is implicitly a duplicate of a recently addressed question: stackoverflow.com/questions/48100957/… . Perhaps you should fix your non-reprex-Q and write your own answer. - IRTFM
sorry how would I go about doing that with dplyr and what does a flag mean, I am quite new to R - Justin SlingerVanland
I do not want to remove these data points, I just don't want regression lines drawn on them - Justin SlingerVanland
e.g. library(tidyverse); ggplot(mtcars, aes(hp, mpg, color = factor(cyl))) + geom_point() + geom_smooth(data = mtcars %>% group_by(cyl) %>% filter(n() > 10), method = 'lm') - alistaire

1 Answers

3
votes

Using the data you shared and calling it df:

df = group_by(df, Species, Year, Parasites) %>%
    mutate(n = n(),
           LogLength = log(`Total Length (cm)`),
           LogWeight = log(`Total Wt (g)`))

ggplot(df, aes(
      x = LogLength,
      y = LogWeight,
      shape = factor(Parasites),
      color = factor(Parasites)
    )) +
    geom_point() + 
    geom_smooth(data = filter(df, n > 3), method = "lm") +
    theme_classic()

enter image description here

I'll leave the label adjustments and such up to you.