简体   繁体   English

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

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

I'm plotting a correlation scatter plot, where my data frame has time data and the start year is arbitrary.我正在绘制相关散点图,其中我的数据框有时间数据,开始年份是任意的。 In this case now I have the following R code在这种情况下,现在我有以下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")

which gives the following output:这给出了以下输出:

在此处输入图片说明

My problem lies in the color palette.我的问题在于调色板。 I use the Greens color palette, which plots data from 2020 in a darker green than data from 2019, which I would like to keep as it is.我使用Greens调色板,它以比 2019 年的数据更深的绿色绘制 2020 年的数据,我想保持原样。

Nevertheless, I would like it to start with the darker shades of green, eg data from 2020 with the green of the red arrow, data from 2019 with the green of the blue arrow.不过,我希望它从较深的绿色阴影开始,例如 2020 年的数据带有红色箭头的绿色,2019 年的数据带有蓝色箭头的绿色。

在此处输入图片说明

How can I do this?我怎样才能做到这一点?

You can use scale_color_manual to set custom colors:您可以使用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")

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM