如何设置调色板,使其从最暗的颜色开始,旧数据比当前数据浅

How do you set the color palette so that it starts with the darkest color, where older data being lighter than current

我正在绘制相关散点图,其中我的数据框包含时间数据并且起始年份是任意的。在这种情况下,现在我有以下 R 代码

## Set seed for randomness in dummy data: ##
set.seed(123)

## Create data frame: ##
df.Data <- data.frame(date = seq(as.Date('2019-01-01'), by = '1 day', length.out = 650),
                      DE = rnorm(650, 2, 1), AT = rnorm(650, 5, 2))
corPearson <- cor.test(x = df.Data$DE, y = df.Data$AT, method = "pearson")

df.Data$year <- format(as.Date(df.Data$date), '%Y')
  
## PLOT: ##
p <- ggplot(data = df.Data, aes(x = DE, y = AT, group = 1)
      ) +
      geom_point(aes(color = year)) + 
      geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
      theme_classic() +
      theme(legend.position = "none") +
      theme(panel.background = element_blank()) +
      scale_colour_brewer(palette = 'Greens') + 
      xlab("PEGAS TTF M1") +
      ylab("EEX DEB M1") +
      ggtitle("Correlation Scatter Plot (Pearson)") +
      theme(plot.title = element_text(hjust = 0.5, face = "bold"))
    
    ## Correlation plot converting from ggplot to plotly: #
    CorrelationPlot <- plotly::ggplotly(p, tooltip = "text")

给出以下输出:

我的问题在于调色板。我使用 Greens 调色板,它以比 2019 年的数据更深的绿色绘制 2020 年的数据,我希望保持原样。

尽管如此,我还是希望它从较深的绿色阴影开始,例如红色箭头为绿色的 2020 年数据,蓝色箭头为绿色的 2019 年数据。

我该怎么做?

您可以使用 scale_color_manual 设置自定义颜色:

library(ggplot2)
library(RColorBrewer)

## Set seed for randomness in dummy data: ##
set.seed(123)

## Create data frame: ##
df.Data <- data.frame(date = seq(as.Date('2019-01-01'), by = '1 day', length.out = 650),
                      DE = rnorm(650, 2, 1), AT = rnorm(650, 5, 2))
corPearson <- cor.test(x = df.Data$DE, y = df.Data$AT, method = "pearson")

df.Data$year <- format(as.Date(df.Data$date), '%Y')

## PLOT: ##
p <- ggplot(data = df.Data, aes(x = DE, y = AT, group = 1)
) +
  geom_point(aes(color = year)) + 
  geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
  theme_classic() +
  theme(legend.position = "none") +
  theme(panel.background = element_blank()) +
  scale_color_manual(values=colorRampPalette(brewer.pal(n = 8, name = "Greens")[7:8])( length(unique(df.Data$year)) )) + 
  xlab(df.Data$DE) +
  ylab(df.Data$AT) +
  ggtitle("Correlation Scatter Plot (Pearson)") +
  theme(plot.title = element_text(hjust = 0.5, face = "bold"))

p

## Correlation plot converting from ggplot to plotly: #
CorrelationPlot <- plotly::ggplotly(p, tooltip = "text")