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

时间:2014-12-01 02:52:09

标签: 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'))

结果情节有一个传说,但它的类型2&#39;部分并不反映fill值的范围 - 是否按设计?

sample plot

4 个答案:

答案 0 :(得分:6)

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

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

关键是将图例中的形状更改为其中一个可以填充&#39;。

答案 1 :(得分:4)

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

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

guide_legend(...)override_aes一起使用可以影响指南的外观(图例)。黑客就是我们在这里&#34;压倒&#34;指南中的填充颜色首先应该具有颜色。

答案 2 :(得分:2)

我玩了数据并提出了这个想法。我首先在第一个geom_point中指定了形状。然后,我把形状弄空了。这样,轮廓保持黑色。第三,我手动分配了特定的形状。最后,我填写了符号。

ggplot(data=df, aes(x=xx, y=yy)) +
geom_point(aes(shape = type3), size = 5.1) + # Plot with three types of shape first
scale_shape(solid = FALSE) + # Make the shapes empty
scale_shape_manual(values=c(24,25,21)) + # Assign specific types of shape
geom_point(aes(color = type2, fill = type2, shape = type3), size = 4.5)

enter image description here

答案 3 :(得分:0)

我不确定你想要的是什么样的?

ggplot(df,aes(x=xx,y=yy))+
  geom_point(aes(shape=type3,color=type2,fill=type2),size=5)+
  scale_shape_manual(values=c(24,25,21))

Ggplot fill-color-shape

相关问题