带有 ggplot2 的散点图由 r 中的特定日期间隔着色

Scatter plot with ggplot2 colored by specific dates interval in r

我正在尝试根据日期(更具体地说是年份)为散点图分配不同的颜色。 这是我的数据集的样子:

> dput(head(CORt_r100_stack_join_fspec,10))
structure(list(Date = structure(c(16779, 16834, 16884, 16924, 
16973, 16997, 17031, 17184, 17214, 17254), class = "Date"), meanNDVIN_int = c(0.677501157246889, 
0.632728796482024, 0.578636981692124, 0.547002029242488, 0.632635423362751, 
NA, 0.699596252720458, 0.670059391804396, 0.643347941166436, 
0.674034259709311), meanNDVIW_int = c(0.784142418592418, 0.652437451242156, 
0.648319814752948, 0.593432266488189, 0.767890365415717, NA, 
0.779249089832163, 0.71974944410843, 0.715777992826006, 0.685045115352089
), meanNDVIE_int = c(0.703614512017928, 0.701963337684803, 0.488628353756438, 
0.631309466083632, 0.781589421376217, NA, 0.799663418920722, 
0.78910564747191, 0.710962969930836, 0.715644011856453), meanNDVINr_int_f = c(0.677501157246889, 
0.632728796482024, 0.578636981692124, 0.547002029242488, 0.632635423362751, 
0.687343078509066, 0.699596252720458, 0.670059391804396, 0.643347941166436, 
0.674034259709311), meanNDVIWr_int_f = c(0.784142418592418, 0.652437451242156, 
0.648319814752948, 0.593432266488189, 0.767890365415717, 0.749505859407419, 
0.779249089832163, 0.71974944410843, 0.715777992826006, 0.685045115352089
), meanNDVIEr_int_f = c(0.703614512017928, 0.701963337684803, 
0.488628353756438, 0.631309466083632, 0.781589421376217, 0.625916155640988, 
0.799663418920722, 0.78910564747191, 0.710962969930836, 0.715644011856453
), NDVI_N = c(0.17221248, 0.644239685, 0.57222623, 0.558666635, 
0.51654034, 0.42053949, 0.396706695, 0.641767447, 0.641008268, 
0.662841949), NDVI_W = c(0.08182944, 0.69112807, 0.637699375, 
0.629429605, 0.658829525, 0.60621678, 0.57186129, 0.72636742, 
0.724193596, 0.738424976), NDVI_E = c(0.17135712, 0.659222803, 
0.58665977, 0.573081253, 0.533498035, 0.437643585, 0.412841468, 
0.652057206, 0.651854988, 0.670345511), NDVI_U = c(0.40520304, 
0.578414833, 0.455746833, 0.428289893, 0.208847548, 0, 0, 0.475193691, 
0.478691084, 0.505043773)), row.names = c(NA, 10L), class = "data.frame")

我一直在密谋 meanNDVIN_int 反对 NDVI_N 使用此代码:

ggplot(CORt_r100_join_fspec_2NDVIday,aes(x=NDVI_N)) + 
  geom_point(aes(y=meanNDVIN_int), colour="red")
  theme_bw()+
  ylab("meanNDVIN_int")+
  xlab("NDVI_N") 

现在我想根据它们的年份、2015 年、2016 年和 2017 年为每个点涂上不同的颜色(无论颜色如何)。 我已经使用 scale_color_manual 函数来引入日期,但到目前为止没有成功。

任何帮助将不胜感激。

我用 lubridate 创建了一个 year 变量并将其存储为 factor 用于 discrete 着色。您只是错过了在 aes() 内移动 color 以通过 year.

为其着色
# Add year Variable;
CORt_r100_stack_join_fspec <- CORt_r100_stack_join_fspec %>% mutate(
        year = as.factor(lubridate::year(Date))
)


# Plot;
ggplot(CORt_r100_stack_join_fspec,aes(x=NDVI_N)) + 
        geom_point(aes(y=meanNDVIN_int, color = year)) +
        theme_bw() +
        ylab("meanNDVIN_int")+
        xlab("NDVI_N") 

注意: 您提供的 data 和命名与您的 plot 调用中的不同。所以我将 CORt_r100_join_fspec_2NDVIday 更改为 CORt_r100_join_fspec_2NDVIday 以使 plotmutate 正常运行。

这里有一个替代方案,您 substring 来自 Date 的前 4 个字符在 color

df
ggplot(df,aes(x=NDVI_N)) + 
  geom_point(aes(y=meanNDVIN_int, color=substring(Date,1,4))) +
  labs(color="Year")+
theme_bw()+
  ylab("meanNDVIN_int")+
  xlab("NDVI_N")