I wanted to construct a manual legend, using ggplot in R.
Following this post: Construct a manual legend for a complicated plot, I constructed a manual legend for my plot.(reproducible example at bottom of post)
I'm not sure what I'm doing wrong, but the legends do not show. The most common answer out on the internet is appointing a color or fill inside your aesthetic, but I believe I did that (?). I'm working with two different dataframes, might that be the issue?
Any help is nice, here's the code (data below):
#plot effect of external ROI negative influence
cols <- c("Yes"="#4730ff","No"="#07BEB8")
EP_ROI <- ggplot() +
geom_ribbon(data=external_ROI,aes(x=month,ymin=div - divsd, ymax=div + divsd,fill="No"),alpha=0.12,fill="#4730ff") +
geom_line(data=external_ROI,aes(x=month,y=div,group=1,colour="Yes"),colour="#4730ff",size=1) +
geom_vline(aes(xintercept=73),colour="#4730ff",linetype="dashed") +
geom_vline(aes(xintercept=130),colour="#4730ff",linetype="dashed") +
labs(y="return on investment (ROI)", x="time (months)") +
geom_ribbon(data=noexternal_ROI,aes(x=month,ymin=div - divsd, ymax=div + divsd,fill="No"),alpha=0.12,fill="#07BEB8") +
geom_line(data=noexternal_ROI,aes(x=month,y=div,group=1,colour="No"),colour="#07BEB8",size=1) +
annotate(x=103, y=7.3,label=paste("external ROI\ninfluence period"), geom="text", color="#4730ff", size=3) +
scale_colour_manual(name="External influence",values=cols) +
scale_fill_manual(name="standard deviation",values=cols)
my data:
> dput(head(external_ROI))
structure(list(month = 0:5, n = c(0, 16.9, 16.9, 16.9, 16.9,
16.9), ids = c(0, 16.9, 16.9, 16.9, 16.9, 16.9), ids_sd = c(0,
7.83075094292788, 7.83075094292788, 7.83075094292788, 7.83075094292788,
7.83075094292788), prods = c(0, 0, 0, 0, 0, 0), prods_sd = c(0,
0, 0, 0, 0, 0), cons = c(0, 0, 0, 0, 0, 0), cons_sd = c(0, 0,
0, 0, 0, 0), strat = c(0, 0, 0, 0, 0, 0), strat_sd = c(0, 0,
0, 0, 0, 0), div = c(0, 0, 0, 0, 0, 0), divsd = c(0, 0, 0, 0,
0, 0), REf = c(0, 0, 0, 0, 0, 0), part = c(NaN, 0, 0.341557936787342,
0.343963785131726, 0.348913136282736, 0.354318810222323), shares = c(0,
1.48042413246047, 1.48042413246047, 1.48042413246047, 1.48042413246047,
1.48042413246047)), .Names = c("month", "n", "ids", "ids_sd",
"prods", "prods_sd", "cons", "cons_sd", "strat", "strat_sd",
"div", "divsd", "REf", "part", "shares"), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))
> dput(head(noexternal_ROI))
structure(list(month = 0:5, n = c(0, 16.9, 16.9, 16.9, 16.9,
16.9), ids = c(0, 16.9, 16.9, 16.9, 16.9, 16.9), ids_sd = c(0,
7.83075094292788, 7.83075094292788, 7.83075094292788, 7.83075094292788,
7.83075094292788), prods = c(0, 0, 0, 0, 0, 0), prods_sd = c(0,
0, 0, 0, 0, 0), cons = c(0, 0, 0, 0, 0, 0), cons_sd = c(0, 0,
0, 0, 0, 0), strat = c(0, 0, 0, 0, 0, 0), strat_sd = c(0, 0,
0, 0, 0, 0), div = c(0, 0, 0, 0, 0, 0), divsd = c(0, 0, 0, 0,
0, 0), REf = c(0, 0, 0, 0, 0, 0), part = c(NaN, 0, 0.347981045925851,
0.346991946002129, 0.349521295784053, 0.354625736233002), shares = c(0,
1.47284681870507, 1.47284681870507, 1.47284681870507, 1.47284681870507,
1.47284681870507)), .Names = c("month", "n", "ids", "ids_sd",
"prods", "prods_sd", "cons", "cons_sd", "strat", "strat_sd",
"div", "divsd", "REf", "part", "shares"), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"))
fill
orcolour
as an aesthetic, so they won't appear in a legend. Yourfill="No"
in the aesthetic is just setting it to a constant value. – Andrew Gustarfill
/color
mapped to a constant insideaes()
but then you wrote over that choice by also puttingfill
/color
as a constant outsideaes()
in, e.g., yourgeom_ribbon()
layer. If you remove your use offill
andcolor
outsideaes()
things should work better. – aosmithdiv
ordivsd
that aren't 0, so there isn't much to visualize as of right now. – camille