如何设置调色板,使其从最暗的颜色开始,较旧的数据比当前的数据更亮 [英] How do you set the color palette so that it starts with the darkest color, where older data being lighter than current

查看:71
本文介绍了如何设置调色板,使其从最暗的颜色开始,较旧的数据比当前的数据更亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制一个相关散点图,其中我的数据框具有时间数据,而起始年份是任意的.在这种情况下,我现在有以下 R 代码

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

## 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 调色板,该调色板将2020年的数据绘制为比2019年的数据更深的绿色,我希望保持不变.

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.

不过,我希望它从较深的绿色阴影开始,例如2020年的数据带有红色箭头的绿色,2019年的数据带有蓝色箭头的绿色.

Nevertheless, I would like it to start with the darker shades of green, e.g. data from 2020 with the green of the red arrow, data from 2019 with the green of the blue arrow.

我该怎么做?

推荐答案

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

You can use scale_color_manual to set custom colors:

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")

这篇关于如何设置调色板,使其从最暗的颜色开始,较旧的数据比当前的数据更亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆