R - ggplot,构建手动图例不显示

时间:2018-06-07 10:45:38

标签: r ggplot2

我想在R。

中使用ggplot构建一个手动图例

在这篇文章之后:Construct a manual legend for a complicated plot,我为我的情节构建了一个手动图例。(帖子底部的可重复示例)

我不确定我做错了什么,但传说没有显示。互联网上最常见的答案是指定一种颜色或填充你的审美,但我相信我做到了(?)。我正在处理两个不同的数据框,这可能是个问题吗?

任何帮助都很好,这里是代码(下面的数据):

#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)

我的数据:

> 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"))

1 个答案:

答案 0 :(得分:2)

如评论中所述,图例不会显示,因为您还在aes调用之外设置颜色和填充,这会覆盖您想要的颜色分配。

最简单的方法是将您的数据合并到一个整洁的数据框中,然后使用ggplot更好地工作,并使代码更易于阅读。

# create fake dataframes
external_ROI <- data.frame(month=1:168, div = c(rnorm(72, 80,5), rnorm(58, 100, 5), rnorm(38, 80, 5)), divsd = rnorm(168, 3, 1))
noexternal_ROI <- data.frame(month=1:168, div = rnorm(168, 80,5), divsd = rnorm(168, 4, 1))
# combine into one dataframe that is tidy
external_ROI$influence <- "No"
noexternal_ROI$influence <- "Yes"
combined.df <- rbind(external_ROI, noexternal_ROI)

#example
library(ggplot2)
#plot effect of external ROI negative influence
cols <- c("Yes"="#4730ff","No"="#07BEB8")

ggplot(data = combined.df) +
  geom_ribbon(aes(x = month, ymin = div - divsd, ymax = div + divsd, 
                                      fill = influence), alpha = 0.12) +
  geom_line(aes(x = month, y = div, colour = influence,), 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)") +
  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)

如果您不想在您的环境中创建额外的对象,您还可以使用管道将它们组合并在一次调用中绘图。 ˚F