geom_point具有不同的填充和形状图例

时间:2015-07-07 13:36:59

标签: r ggplot2

嗯,也许这是温度,或者我再一次看不到明显的......

这是我的代码:

library(ggplot2)

p <- ggplot()
p <- p + geom_point(aes(x = 1, y=1,bg = "I", group = "B"),pch = 21, size = 20, color=NA)
p <- p + geom_point(aes(x = 1, y=1.125,bg = "I", group = "B" ),pch = 22, size = 20, color=NA)
p <- p + geom_point(aes(x = 0.75, y=1.125,bg = "II", group = "A" ),pch = 22, size = 20, color=NA)
p <- p + geom_point(aes(x = 0.85, y=1.125,bg = "III", group = "A" ),pch = 22, size = 20, color=NA)
p <- p + scale_fill_manual(values= c("darkred", "darkblue", "darkgreen"), guide=guide_legend(override.aes = list(shape = 23)))
#p <- p + scale_fill_manual(values= c("darkred", "darkblue", "darkgreen"), guide=guide_legend(inherit.aes = FALSE))
p <- p + scale_shape_manual(labels = c("circle", "rectangle"),values = c(21, 22))
p

我想要实现的基本上是两个传说,一个反映颜色,在这个例子中有三种不同的颜色(“I”,“II”和“III”),以及两种不同类型的形状“矩形”和“圆形”,永远不会超过这两种不同的形状。

不幸的是还有一些额外的限制......我不能使用审美color因为我也使用geom_segment以某种方式连接形状,这是第二个约束,我必须使用ggplot2。

但我不能产生这两个传说,任何帮助都表示赞赏......

1 个答案:

答案 0 :(得分:4)

为什么不将所有积分存储在数据框中?它非常适合:

df <- data.frame(x = c(1, 1, 0.75, 0.85), 
                 y = c(1, 1.125, 1.125, 1.125), 
                 nr = c("I", "I", "II", "III"), 
                 sh = c("B", "B", "A", "A"))

现在您可以轻松查看所需的映射:

ggplot(df, aes(x, y, fill = nr, shape = sh)) + 
  geom_point(size = 20, color = NA) + 
  scale_shape_manual(labels = c("circle", "rectangle"), values = c(21, 22), 
                     guide = guide_legend(override.aes = list(colour = 1))) + 
  scale_fill_manual(values = c("darkred", "darkblue", "darkgreen"), 
                    guide = guide_legend(override.aes = list(shape = 23)))

enter image description here