图例中未显示“填充”比例 [英] `fill` scale is not shown in the legend

查看:179
本文介绍了图例中未显示“填充”比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的虚拟代码:

  set.seed(1)
df< - data.frame (xx =样本(10,6),
yy =样本(10,6),
type2 = c('a','b','a','a','b' ,'b'),
type3 = c('A','C','B','A','B','C')

ggplot(data = af(x = x,y = yy))+
geom_point(aes(shape = type3,fill = type2),size = 5)+
scale_shape_manual(values = c(24 ,25,21))+
scale_fill_manual(values = c('green','red'))

生成的图有一个图例,但它是'type2'部分不反映 fill 值的缩放比例 - 是否由设计决定?



解决方案

我知道这是一个古老的线程,但我遇到了这个确切的问题,并希望在这里发布给像我这样的人。尽管接受的答案有效,但风险较小,清洁的方法是:

$ g $ p $ ggplot2
ggplot(data = df,mapping = aes(x = xx,y = yy))+
geom_point(aes(shape = type3,fill = type2),size = 5)+
scale_shape_manual(values = c(24, 25,21))+
scale_fill_manual(values = c(a ='green',b ='red'))+
guides(fill = guide_legend(override.aes = list(shape = 21) ))

关键在于将图例中的形状更改为可以具有填充'。


Here is my dummy code:

set.seed(1)
df <- data.frame(xx=sample(10,6), 
                 yy=sample(10,6), 
                 type2=c('a','b','a','a','b','b'),
                 type3=c('A','C','B','A','B','C')
                 )
ggplot(data=df, mapping = aes(x=xx, y=yy)) + 
geom_point(aes(shape=type3, fill=type2), size=5) +
  scale_shape_manual(values=c(24,25,21)) +
  scale_fill_manual(values=c('green', 'red'))

Resulting plot has a legend but it's 'type2' section doesn't reflect scale of fill value - is it by design?

解决方案

I know this is an old thread, but I ran into this exact problem and want to post this here for others like me. While the accepted answer works, the less risky, cleaner method is:

library(ggplot2)
ggplot(data=df, mapping = aes(x=xx, y=yy)) + 
  geom_point(aes(shape=type3, fill=type2), size=5) +
  scale_shape_manual(values=c(24,25,21)) +
  scale_fill_manual(values=c(a='green',b='red'))+
  guides(fill=guide_legend(override.aes=list(shape=21)))

The key is to change the shape in the legend to one of those that can have a 'fill'.

这篇关于图例中未显示“填充”比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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