我正在使用ggplot绘制一系列线条并在它们之间加阴影。有10条线,但只有5个阴影区域。我想要5个阴影区域的图例。目前没有任何传说。我以为是线
scale_color_manual(name="test",labels = c("90%","70%","50%","30%","10%"),
values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))
将添加带有阴影区域的手动颜色栏,但是什么也没有显示。完整的绘图代码如下。谢谢
Exposure_by_Month_10perc=Exposure_by_Month[,c(1,seq(3,length(monthQuant[1,])+1,4))]
dfm <- melt(Exposure_by_Month_10perc, id.vars = "Date")
dfm$value=dfm$value/1000000
colnames(dfm)=c("Date","Confidence Interval","value")
ggplot(Exposure_by_Month_10perc, aes(x=Date, y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.15",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.25",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.35",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.45",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.55",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.65",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.75",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.85",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.95",3])) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.95",3], ymax = dfm[dfm$`Confidence Interval`== "0.05",3]), fill = "Darkolivegreen1", alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.85",3], ymax = dfm[dfm$`Confidence Interval`== "0.15",3]), fill = "Darkolivegreen2", alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.75",3], ymax = dfm[dfm$`Confidence Interval`== "0.25",3]), fill = "Darkolivegreen3", alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.65",3], ymax = dfm[dfm$`Confidence Interval`== "0.35",3]), fill = "Darkolivegreen4", alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.55",3], ymax = dfm[dfm$`Confidence Interval`== "0.45",3]), fill = "Darkolivegreen", alpha = .5) +
scale_x_date(labels = date_format("%b-%Y"),breaks='2 months') +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "XXXX TITLE", subtitle = "XXX SUBTITLE") +
xlab("Month") +
ylab("Value, $M") +
scale_y_continuous(label=dollar_format(),breaks = round(seq(min(dfm$value), max(dfm$value), by = 1),0)) +
geom_line(aes(y = 0*dfm[dfm$`Confidence Interval`== "0.95",3]),linetype = "dashed") +
scale_color_manual(name="test",labels = c("90%","70%","50%","30%","10%"),
values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))
###why no legend
> head(Exposure_by_Month_10perc)
# A tibble: 6 x 11
Date `0.05` `0.15` `0.25` `0.35` `0.45` `0.55` `0.65` `0.75` `0.85` `0.95`
<date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2018-10-01 -1.44e6 -2.58e6 -3.42e6 -4.27e6 -5.15e6 -6.01e6 -7.01e6 -8.11e6 -9.51e6 -1.21e7
2 2018-11-01 -5.36e4 -1.42e6 -2.35e6 -3.35e6 -4.40e6 -5.67e6 -7.09e6 -8.66e6 -1.07e7 -1.50e7
3 2018-12-01 6.63e5 -7.65e5 -1.87e6 -3.05e6 -4.32e6 -5.82e6 -7.62e6 -9.68e6 -1.25e7 -1.87e7
4 2019-01-01 8.34e5 -9.67e5 -1.90e6 -2.68e6 -3.64e6 -4.73e6 -5.59e6 -6.94e6 -8.66e6 -1.19e7
5 2019-02-01 1.16e6 -4.70e5 -1.52e6 -2.13e6 -3.10e6 -4.15e6 -5.13e6 -6.53e6 -8.31e6 -1.20e7
6 2019-03-01 1.88e6 -6.02e3 -1.40e6 -2.10e6 -3.26e6 -4.49e6 -5.72e6 -7.46e6 -9.74e6 -1.38e7
[无图例] 1
答案 0 :(得分:0)
尝试一下:
scale_color_manual(guide="legend", labels = c("90%","70%","50%","30%","10%"),
values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))
答案 1 :(得分:0)
这是通过将fill = XXX放置在geom_ribbon函数的aes括号内来解决的,如下所示:
ggplot(Exposure_by_Month_10perc, aes(x=Date, y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.05",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.15",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.25",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.35",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.45",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.55",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.65",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.75",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.85",3])) +
geom_line(aes(y = dfm[dfm$`Confidence Interval`== "0.95",3])) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.95",3], ymax = dfm[dfm$`Confidence Interval`== "0.05",3], fill = "Darkolivegreen1"), alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.85",3], ymax = dfm[dfm$`Confidence Interval`== "0.15",3], fill = "Darkolivegreen2"), alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.75",3], ymax = dfm[dfm$`Confidence Interval`== "0.25",3], fill = "Darkolivegreen3"), alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.65",3], ymax = dfm[dfm$`Confidence Interval`== "0.35",3], fill = "Darkolivegreen4"), alpha = .5) +
geom_ribbon(aes(ymin = dfm[dfm$`Confidence Interval`== "0.55",3], ymax = dfm[dfm$`Confidence Interval`== "0.45",3], fill = "Darkolivegreen"), alpha = .5) +
scale_x_date(labels = date_format("%b-%Y"),breaks='2 months') +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Tullow Hedging Portfolio", subtitle = "Value at Risk (VAR) Analysis") +
xlab("Month") +
ylab("Value at Risk, $M") +
scale_y_continuous(label=dollar_format(),breaks = round(seq(min(dfm$value), max(dfm$value), by = 1),0)) +
geom_line(aes(y = 0*dfm[dfm$`Confidence Interval`== "0.95",3]),linetype = "dashed") +
scale_fill_manual(name="Confidence Interval",labels = c("90%","70%","50%","30%","10%"),
values = c("Darkolivegreen1", "Darkolivegreen2","Darkolivegreen3","Darkolivegreen4","Darkolivegreen"))