geg_lota / geom_ribbon的ggplot2图例未显示

时间:2017-12-10 21:59:32

标签: r ggplot2

以下情节的图例未显示。我想要2个传奇条目。一个用于紫色,一个用于绿色。

library(ggplot2)

index <- c(1,2,3,4,5)
size <- c(10,20,25,40,45)
df <- data.frame(index, size)

p <- ggplot(df, aes(x=index, y=size, fill=size)) 

p +
  geom_ribbon(aes(fill="dfg", ymin=size, ymax=df$size[nrow(df)]), fill="#BEAED4", alpha=0.2, color="#BEAED4") +
  geom_area(aes(fill="abc"), fill="#84CA72", alpha=0.2, colour="#84CA72") +
  scale_fill_manual(name="skdjf", breaks=c("abc", "dfg"), values=c("red", "blue"), labels = c("cat1", "cat2"))

Recharts

PS:我尝试了很多东西,但似乎无法让它发挥作用。

1 个答案:

答案 0 :(得分:2)

您在美学内外分配fill

试试这个

library(tidyverse)

data.frame(index = c(1,2,3,4,5), size = c(10,20,25,40,45)) %>% 
ggplot(aes(index, size)) + 
  geom_ribbon(aes(ymin = min(size), ymax = max(size), fill = 'dfg', color = 'dfg'), alpha = 0.2) + 
  geom_area(aes(fill = 'abc', color = 'abc'), alpha = 0.2) +
  scale_fill_manual(name = 'a name', values = c("#BEAED4","#84CA72")) +
  scale_color_manual(name = 'a name', values = c("#BEAED4","#84CA72"))