`fill`比例未显示在图例中

Vas*_*y A 9 r ggplot2

这是我的虚拟代码:

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'))
Run Code Online (Sandbox Code Playgroud)

结果图有一个图例,但它的'type2'部分并不反映fill价值尺度- 它是否符合设计?

样本图

小智 9

我知道这是一个老线程,但我遇到了这个确切的问题,并想在这里发布这个像我这样的人.虽然接受的答案有效,但风险较小,清洁方法较少:

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)))
Run Code Online (Sandbox Code Playgroud)

关键是将图例中的形状更改为可以具有"填充"的形状之一.


jlh*_*ard 5

这是一个不同的解决方法。

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(colour=c(a="green",b="red"))))
Run Code Online (Sandbox Code Playgroud)

使用guide_legend(...)withoverride_aes是一种影响指南(图例)外观的方法。黑客是在这里我们用它们最初应该具有的颜色“覆盖”指南中的填充颜色。