首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置调色板,使其从最暗的颜色开始,其中较旧的数据比当前数据亮

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

Stack Overflow用户
提问于 2020-10-01 16:01:26
回答 1查看 86关注 0票数 1

我正在绘制一个相关散点图,其中我的数据框包含时间数据,起始年份是任意的。在本例中,我现在有以下R代码

代码语言:javascript
复制
## 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年的数据使用蓝色箭头的绿色。

我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-01 16:33:04

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

代码语言:javascript
复制
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")

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64151267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档