这是我的代码:
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。
但我不能产生这两个传说,任何帮助都表示赞赏......
答案 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)))