R 指定渐变背景色并与线条颜色分离(ggplot2)

R 指定渐变背景色并与线条颜色分离(ggplot2),r,ggplot2,background-color,R,Ggplot2,Background Color,我正在尝试创建一个基于另一个变量的渐变色背景的线条图(与本文非常类似)。不同的是,我有多行是基于不同的因素水平。这是我的密码: ggplot(data=cl, aes(x=date, y=value)) + geom_rect(aes(xmin=date,xmax=date+1,ymin=min(value),ymax=max(value), fill=StringencyIndex2)) + geom_line(aes(colour=dominio),

我正在尝试创建一个基于另一个变量的渐变色背景的线条图(与本文非常类似)。不同的是,我有多行是基于不同的因素水平。这是我的密码:

ggplot(data=cl, aes(x=date, y=value)) +
  geom_rect(aes(xmin=date,xmax=date+1,ymin=min(value),ymax=max(value), 
                fill=StringencyIndex2)) +
  geom_line(aes(colour=dominio), size=1)
这就产生了这个图

如何将背景色更改为蓝色渐变,并对线条使用不同的调色板

非常感谢

###
###
# Generate data 
###
set.seed(1)
date <- seq(as.Date("01Jan20", "%d%b%Y"),as.Date("28May20", "%d%b%Y"), by=1)
dts <- data.frame(date=date,
                  value=cumsum(rnorm(length(date))),
                  dominio=sample(LETTERS[1:5],length(date),replace=T),
                  StringencyIndex2=cut(runif(length(date)), breaks=seq(0,1,0.25)))
dts$dominio <- factor(dts$dominio, labels=c("Comercio y ocio","Alimentacion y farmacia",
                                            "Transporte publico","Trabajo","Residencial"))

#########
# Define colors for the background like in scale_fill_gradient
#########
pal <- colorRampPalette(c("#132B43","#56B1F7"))
cols <- pal(length(unique(dts$StringencyIndex2)))

ggplot(data=dts, aes(x=date, y=value)) +
  geom_rect(aes(xmin=date,xmax=date+1,ymin=min(value),ymax=max(value), 
                fill=StringencyIndex2)) +
  geom_line(aes(colour=dominio), size=1) +
  scale_fill_manual(values=cols) +
  #########
  # Define line colors using scale_color_manual  
  #########
  scale_color_manual(values=c("Comercio y ocio"="red",
                              "Alimentacion y farmacia"="white",
                              "Transporte publico"="yellow",
                              "Trabajo"="green",
                              "Residencial"="cyan")) +
  theme_bw()
#生成数据 ### 种子(1)
约会真棒!谢谢。@v.white很高兴能帮助你。请你接受我的回答并投票表决好吗?