添加轮廓颜色作为点属性 [英] Adding outline color as point attribute

查看:53
本文介绍了添加轮廓颜色作为点属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个愚蠢的 data.frame :

df <- read.table(textConnection(
"pole medal  bag     x     y
north gold   paper   0.852 0.423
north gold   plastic 0.277 0.055
north silver paper   0.257 0.211
north silver plastic 0.457 0.614
north bronze paper   0.825 0.299
north bronze plastic 0.672 0.126
south gold   paper   0.482 0.764
south gold   plastic 0.603 0.869
south silver paper   0.327 0.451
south silver plastic 0.147 0.672
south bronze paper   0.140 0.466
south bronze plastic 0.833 0.325
"), header = TRUE)

我知道如何绘制这些数据的散点图,该方法使用颜色和形状来表示两个因子属性.例如:

I know how to plot a scatterplot for these data in a way that uses color and shape to indicate two of the factor attributes; for example:

library(ggplot2)
ggplot(data = df, aes(x = x, y = y)) +
geom_point(size = 4, aes(shape = pole, color = bag))

我想再添加一个点特征以指示第三个因素属性(在本例中为 medal ).想到的一种可能性是彩色轮廓.

I would like to add one more point feature to indicate a third factor attribute (in this case medal). The one possibility that comes to mind is a colored outline.

是否有方便的方法来做到这一点?(问题的一个棘手的方面是,轮廓的调色板必须与用于点填充的调色板不同,因为对于每个点,轮廓和填充必须在视觉上是可区分的.)

Is there a convenient way to do this? (One tricky aspect of the problem is that the color palette for the outlines must be distinct from that used for the point fills, because, for each point, the outline and the fill must be visually distinguishable.)

更新:

当我尝试使用Gregor的建议时,这些要点看起来很正确,但传说却被弄糊涂了:

When I try Gregor's suggestion, the points look right, but the legend is messed up:

推荐答案

可以!帮助中有一个关于?geom_point 的示例:

Yes you can! There is an example in the help for ?geom_point:

# For shapes that have a border (like 21), you can colour the inside and
# outside separately. Use the stroke aesthetic to modify the width of the
# border
ggplot(mtcars, aes(wt, mpg)) +
  geom_point(shape = 21, colour = "black", fill = "white", size = 5, stroke = 5)

在这种情况下,您将要使用形状21(带轮廓的圆)和24(带轮廓的三角形)的形状,

In your case you'll want to use shapes 21 (circles with outline) and 24 (triangles with outline) like this:

ggplot(data = df, aes(x = x, y = y)) +
  geom_point(aes(shape = pole, color = medal, fill = bag),
             size = 4, stroke = 2) + 
  scale_shape_manual(values = c(21, 24))

请注意,当同时使用时, fill 对应于点的中心,而 color 则对应于轮廓.您可以通过设置描边来更改轮廓的粗细.

Notice that, when using both, fill corresponds to the center of the points and color to the outline. You can change the weight of the outline by setting stroke.

如评论中所述,您必须做一些额外的调整才能使图例正确.添加到上面的情节中:

As noted in the comments, you'll have to do some extra tweaking to get the legend right. Adding to the above plot:

fill_col = c("pink", "white")
outline_col = c("brown", "gold", "grey74")

scale_fill_manual(values = fill_col) + 
scale_color_manual(values = outline_col) +
guides(fill = guide_legend(override.aes = list(color = fill_col)),
       color = guide_legend(override.aes = list(shape = 21)))

这篇关于添加轮廓颜色作为点属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆