R 如何在ggplot2的重叠区域打印中将打印颜色从默认更改为自定义颜色

R 如何在ggplot2的重叠区域打印中将打印颜色从默认更改为自定义颜色,r,R,我在ggplot2中创建了以下重叠区域图: head(MP_rates_dateformat) Month repo revrepo bankrate CRR Callrate WPI GDP FED width 1 2001-04-01 9.00 6.75 7.00 8.00 7.49 5.41 4.6 4.50 225 2 2001-05-01 8.75 6.50 7.00 7.50 8.03 5.6

我在ggplot2中创建了以下重叠区域图:

head(MP_rates_dateformat) 
         Month repo revrepo bankrate  CRR Callrate   WPI  GDP  FED width
1   2001-04-01 9.00    6.75     7.00 8.00     7.49  5.41  4.6 4.50   225
2   2001-05-01 8.75    6.50     7.00 7.50     8.03  5.60  4.6 4.00   225
3   2001-06-01 8.50    6.50     7.00 7.50     7.24  5.30  4.6 3.75   200
4   2001-07-01 8.50    6.50     7.00 7.50     7.19  5.23  5.3 3.75   200
5   2001-08-01 8.50    6.50     7.00 7.50     6.94  5.41  5.3 3.50   200

library("tidyr")

long_DF<- MP_rates_dateformat[,1:3] %>% gather(variable, value, -Month)
head(long_DF)

       Month variable value
1 2001-04-01     repo  9.00
2 2001-05-01     repo  8.75
3 2001-06-01     repo  8.50
4 2001-07-01     repo  8.50
5 2001-08-01     repo  8.50
6 2001-09-01     repo  8.50

library("ggplot2") 

ggplot(data = long_DF, aes(x = Month)) +
geom_area(aes(y = value, fill = variable), position = "identity") + 
labs(fill="") + xlab('\nYears') + ylab('LAF Rates (%)\n') +   labs(title="Overlapping - Repo & Reverse Repo\n")+ geom_line(data = MP_rates_dateformat, aes(x=Month, y=width/100, color = "width"),size=1.05)+ labs(color="")
head(MP\u rates\u日期格式)
月回购利率回购银行利率CRR赎回利率WPI GDP美联储宽度
1   2001-04-01 9.00    6.75     7.00 8.00     7.49  5.41  4.6 4.50   225
2   2001-05-01 8.75    6.50     7.00 7.50     8.03  5.60  4.6 4.00   225
3   2001-06-01 8.50    6.50     7.00 7.50     7.24  5.30  4.6 3.75   200
4   2001-07-01 8.50    6.50     7.00 7.50     7.19  5.23  5.3 3.75   200
5   2001-08-01 8.50    6.50     7.00 7.50     6.94  5.41  5.3 3.50   200
图书馆(“tidyr”)
long_DF%聚集(变量,值,-月)
头部(长头)
月份变量值
1 2001-04-01回购协议9.00
2 2001-05-01回购协议8.75
3 2001-06-01回购协议8.50
4 2001-07-01回购协议8.50
5 2001-08-01回购协议8.50
6 2001-09-01回购协议8.50
图书馆(“ggplot2”)
ggplot(数据=长时,aes(x=月))+
几何图形面积(aes(y=值,填充=变量),位置=“标识”)+
实验室(fill=”“)+xlab('\nYears')+ylab('LAF Rates(%)\n')+labs(title=“Overlapping-Repo&Reverse Repo\n”)+geom_line(data=MP_Rates\u dateformat,aes(x=Month,y=width/100,color=“width”),size=1.05)+实验室(color=“”)

正如您所看到的,绘图有默认颜色,线条图也有默认颜色。我想说的是黄绿色表示
repo
,淡蓝色表示
revrepo
,深蓝色表示
width
。这些颜色也应该反映在图例中。底线是如何在上面的图中获得自定义颜色

在早期的堆栈溢出qs中,出现了让图例颜色与默认重叠区域绘图相匹配的问题,但这里的问题不同……如何在绘图中获得不同的用户定义颜色,而不是ggplot2提供的默认颜色。

得到了

library("ggplot2") 
ggplot(data = long_DF, aes(x = Month)) +
geom_area(aes(y = value, fill = variable), position = "identity") +   scale_fill_manual(values=c("yellowgreen","dodgerblue")) + labs(fill="") +   xlab('\nYears') + ylab('LAF Rates (%)\n') + 
labs(title="Overlapping - Repo & Reverse Repo\n")+ geom_line(data =  MP_rates_dateformat, aes(x=Month, y=width/100, color = "Width"),size=1.05)+ scale_color_manual(values="darkblue")+ labs(color="")

在早期的堆栈溢出qs中--如何在ggplot2中的重叠区域打印中匹配图例颜色和打印颜色问题是让图例颜色与默认重叠区域打印匹配,但这里的问题不同…如何在打印中获得不同的用户定义颜色,而不是ggplot2中的默认颜色给出。检查
scale\u fill\u manual
:。请参阅对其他问题的评论:
scale\u fill\u manual