使用ggplot2的多个传说

时间:2016-06-19 07:46:03

标签: r ggplot2

我试图在同一个地块上绘制多组数据。例如,下面,我有两组设置实验:" P-0.1"和" P-0.2"如因子B所示。对于每个实验,我有三个输出值" P"," Q"," O",如因子A所示。我有按填充颜色对实验进行分组,并按颜色,线型和形状对输出进行分组。

library(ggplot2)

dat <- data.frame(
    A = factor(c("O", "O", "P", "P", "Q", "Q", "O", "O", "P", "P", "Q", "Q"), levels=c("O", "O", "P", "P", "Q", "Q","O", "O", "P", "P", "Q", "Q")),
    B = factor(c("P-0.1", "P-0.1", "P-0.1", "P-0.1","P-0.1", "P-0.1",  "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2"), levels = c("P-0.1", "P-0.1", "P-0.1", "P-0.1","P-0.1", "P-0.1",  "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2")),
    X = c( 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1),
    Y = c(10, 15, 12, 13, 9, 14, 30, 20, 32, 21, 27, 19)
)

p = ggplot(data=dat, aes(x=X, y=Y, colour=A, size=A, shape=A, linetype=A, fill=B, group=interaction(A,B))) + geom_point() + geom_line() + theme_bw()
p = p + geom_point(size=4, alpha=0) + geom_point(size=4, show.legend=FALSE) + guides(shape = guide_legend(nrow=3, byrow = TRUE, keywidth = 1.5, keyheight = 1), colour = guide_legend(override.aes = list(alpha=1)))

p = p + scale_shape_manual(name="", values=c(21,22,23))
p = p + scale_colour_manual(name="", values=c("#005ccc", "#007700", "#56B4E9"))
p = p + scale_linetype_manual(name="", values=c(0,0,1))
p = p + scale_size_manual(name="", values = c(1, 1, 1))
p = p + scale_fill_manual(name="", values = c("red", "blue"))

上面的代码正确地绘制了图形,但是我在填充比例的图例中得到黑色圆圈,我期待填充红色和蓝色框。如何解决这个问题?

Reference Figure

0 个答案:

没有答案