没有显示geom_area / geom_ribbon的ggplot2图例 [英] ggplot2 legend for geom_area/geom_ribbon not showing

查看:486
本文介绍了没有显示geom_area / geom_ribbon的ggplot2图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下图表的图例未显示。我想要2个图例条目。

  library(ggplot2)

索引<-c(一个用于紫色,一个用于绿色) 1,2,3,4,5)
size< -c(10,20,25,40,45)
df< - data.frame(索引,大小)

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,color =#84CA72)+
scale_fill_manual(name =skdjf,breaks = c(abc,dfg),values = c(red ,blue),labels = c(cat1,cat2))

< a href =https://i.stack.imgur.com/kmDff.png =nofollow noreferrer>



PS:我尝试了很多东西,但似乎无法使它工作。 b $ b

解决方案

您在审美内外分配 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))


The legend for the following plot is not showing. I would like 2 legend entries. One for purple and one for green.

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

PS: I tried a lot of stuff but can't seem to get it to work.

解决方案

You are assigning fill within and outside of the aesthetic

Try this

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

这篇关于没有显示geom_area / geom_ribbon的ggplot2图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆