如何删除ggplot2中的某些回归线,但让其他人使用stat_smooth
或geom_smooth
函数?
我正在绘制鱼类的length ~ weight
关系,比较湖泊,年份,物种和寄生。
我可以绘制所有内容的回归线,例如Parasitized vs Non Parasitized,但是如果一组说Parasitized只有2个点,那么仍然会为它做一个回归线,就像所有其他有3个回归线一样。或更多积分。
我的问题是如何绘制数据以为具有3个或更多点的数据创建回归线,但同时不为仅有两个点的数据创建回归线?
我已将数据和示例图表包含在问题中:
> 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和y轴是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"))
所以我收到了这些错误......
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()),"
答案 0 :(得分:2)
使用您分享的数据并将其调用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()
我会留下标签调整等等。