我有一个ggplot,我想为它添加一个图例,但它只反映颜色,而不是形状。我使用了scale_shape_manual,但它仍然不起作用。这就是我所拥有的
plot <- ggplot(data.frame(labels, yes.percent.avgs.gc, yes.percent.avgs.dp, yes.percent.avgs.gm)) +
geom_point(aes(x=labels, y=yes.percent.avgs.gc, colour="GC"), shape=16, size=5) + # Plot individual points
geom_point(aes(x=labels, y=yes.percent.avgs.dp, colour="DP"), shape=17, size=5) + # Plot individual points
geom_point(aes(x=labels, y=yes.percent.avgs.gm, colour="GM"), shape=18, size=5) + # Plot individual points
geom_smooth(data=data.frame(labels, pred.avgs), aes(x=labels, y=pred.avgs, colour="Prediction Scores"), fill=NA, method=lm, size=1) +
xlab("Bin range") +
ylab("Argument Score") +
theme(legend.position = c(0.8, 0.2))+
scale_shape_manual(name = "Legend",
labels = c("GC", "DP", "GM"),
values = c(16, 17, 18))
plot # show the plot
如何让图例显示颜色和形状。
答案 0 :(得分:2)
显然,问题在于你将三个geom_point
层叠在一起。这会混淆ggplot
,它会在图例中一次性使用所有形状。
解决方案是在绘制数据之前整理数据。由于您没有提供reproducible example,我必须补上。
library("ggplot2")
library("tidyr")
set.seed(123)
labels <- sample(c("L1","L2", "L3", "L4"), 12, replace = TRUE)
yes.percent.avgs.gc <- sample(1:100, 12)
yes.percent.avgs.dp <- sample(1:100, 12)
yes.percent.avgs.gm <- sample(1:100, 12)
tidy.df <- gather(data.frame(labels,
yes.percent.avgs.gc,
yes.percent.avgs.dp,
yes.percent.avgs.gm),
group, value,
yes.percent.avgs.gc, yes.percent.avgs.dp, yes.percent.avgs.gm)
plot <- ggplot(tidy.df) +
geom_point(aes(x=labels, y=value, colour=group, shape=group), size=5) +
xlab("Bin range") +
ylab("Argument Score") +
scale_shape_manual(name = "Legend",
labels = c("GC", "DP", "GM"),
values = c(16, 17, 18)) +
scale_colour_manual(name="Legend",
labels=c("GC", "DP", "GM"),
values=c("Red", "Green", "Blue"))
print(plot)
我放弃了geom_smooth
电话,因为我看不出有和没有情节之间的差异。