使用 scale_shape_manual 时图例中丢失的颜色
Colors lost in legend when using scale_shape_manual
我正在尝试使用填充形状从形状的位置 21 绘制点。
d <- data.frame(expand.grid(a=letters[1:4],
b=factor(1:2)), y=rnorm(8),x=rnorm(8))
这个效果很好:
ggplot(d, aes(x=x, y=y,fill=a,color=a)) +
geom_point(aes(shape=b,fill=a),size=4) +
scale_shape_manual(values=c(21,22))
我想让形状的轮廓颜色为黑色,所以我认为在 aes()
之外设置 color="black" 就可以了:
ggplot(d, aes(x=x, y=y,fill=a,color=a)) +
geom_point(aes(shape=b,fill=a),color="black",size=4) +
scale_shape_manual(values=c(21,22))
这在剧情中有效。但是,图例中的颜色都设置为黑色。
如何得到上面的图例?
出于某种原因,填充图例默认为形状符号 1(实心圆),因此它显示颜色而不是填充美学。将此添加到 ggplot 命令:
+ guides(fill=guide_legend(override.aes=list(shape=21)))
我正在尝试使用填充形状从形状的位置 21 绘制点。
d <- data.frame(expand.grid(a=letters[1:4],
b=factor(1:2)), y=rnorm(8),x=rnorm(8))
这个效果很好:
ggplot(d, aes(x=x, y=y,fill=a,color=a)) +
geom_point(aes(shape=b,fill=a),size=4) +
scale_shape_manual(values=c(21,22))
aes()
之外设置 color="black" 就可以了:
ggplot(d, aes(x=x, y=y,fill=a,color=a)) +
geom_point(aes(shape=b,fill=a),color="black",size=4) +
scale_shape_manual(values=c(21,22))
这在剧情中有效。但是,图例中的颜色都设置为黑色。
如何得到上面的图例?
出于某种原因,填充图例默认为形状符号 1(实心圆),因此它显示颜色而不是填充美学。将此添加到 ggplot 命令:
+ guides(fill=guide_legend(override.aes=list(shape=21)))