Ggplot2 在ggplot中组合数据集的不同部分

Ggplot2 在ggplot中组合数据集的不同部分,ggplot2,Ggplot2,我有一个非常简单的数据集,例如: df <- tibble("FID" = c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2), "NDVI" = c(0.5, 0.55, 0.2, 0.3, 0.4, 0.5, 0.5, 0.5, 0.5, 0.55, 0.7, 0.72, 0.3, 0.35, 0.45, 0.50, 0.60, 0.65, 0.7, 0.7),

我有一个非常简单的数据集,例如:

df <- tibble("FID" = c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2),  
             "NDVI" = c(0.5, 0.55, 0.2, 0.3, 0.4, 0.5, 0.5, 0.5, 0.5, 0.55,
                       0.7, 0.72, 0.3, 0.35, 0.45, 0.50, 0.60, 0.65, 0.7, 0.7),
             "days" = c(-36, -60, 0, 30, 60, 90, 100, 120, 140, 200,
                        -36, -60, 0, 30, 60, 90, 100, 120, 140, 200))
> df
# A tibble: 20 x 3
     FID  NDVI  days
   <dbl> <dbl> <dbl>
 1     1  0.5    -36
 2     1  0.55   -60
 3     1  0.2      0
 4     1  0.3     30
 5     1  0.4     60
 6     1  0.5     90
 7     1  0.5    100
 8     1  0.5    120
 9     1  0.5    140
10     1  0.55   200
11     2  0.7    -36
12     2  0.72   -60
13     2  0.3      0
14     2  0.35    30
15     2  0.45    60
16     2  0.5     90
17     2  0.6    100
18     2  0.65   120
19     2  0.7    140
20     2  0.7    200


我无法找到如何在特定条件下(在本例中,天>0)向第一个图形添加回归线(如第二个图形)

ggplot的酷之处在于,您可以完全忽略
ggplot()
中的参数,并将它们直接传递到
geom.*
中。 所以,你可以这样做:

ggplot() + 
  geom_point(data = df, mapping = aes(x = days, y = NDVI, color = as.factor(FID), fill = as.factor(FID)), alpha=0.5, shape=21, size=5) + 
  geom_smooth(data = filter(df, days > 0), mapping = aes(x = days, y = NDVI, color = as.factor(FID)), method="lm",se=TRUE)
这将导致以下绘图:

关于ggplot最酷的一点是,您可以完全忽略
ggplot()
中的参数,并将它们直接传递到
geom.*
中。 所以,你可以这样做:

ggplot() + 
  geom_point(data = df, mapping = aes(x = days, y = NDVI, color = as.factor(FID), fill = as.factor(FID)), alpha=0.5, shape=21, size=5) + 
  geom_smooth(data = filter(df, days > 0), mapping = aes(x = days, y = NDVI, color = as.factor(FID)), method="lm",se=TRUE)
这将导致以下绘图:

哦,我不知道!!谢谢!哦,我不知道!!谢谢!
ggplot() + 
  geom_point(data = df, mapping = aes(x = days, y = NDVI, color = as.factor(FID), fill = as.factor(FID)), alpha=0.5, shape=21, size=5) + 
  geom_smooth(data = filter(df, days > 0), mapping = aes(x = days, y = NDVI, color = as.factor(FID)), method="lm",se=TRUE)