【问题标题】:Adding outline color as point attribute添加轮廓颜色作为点属性
【发布时间】:2016-12-24 10:16:12
【问题描述】:

这是一个愚蠢的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)

我知道如何为这些数据绘制散点图,使用颜色和形状来指示两个因素属性;例如:

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

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

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

更新:

当我尝试 Gregor 的建议时,点看起来是正确的,但图例却搞砸了:

【问题讨论】:

  • 您可能需要为填充与颜色建立自己的配色方案,以使其看起来不错。加上一些传奇作品。如:scale_fill_manual(values = c("pink", "white")) + scale_color_manual(values = c("brown", "gold", "grey74")) + guides(fill = guide_legend(override.aes = list(color = c("pink", "white"))), color = guide_legend(override.aes = list(shape = 21)))
  • 为什么这个问题被否决了?
  • @kjo,可能是因为geom_point 的文档中对此进行了详细讨论。还有see for example here
  • 示例here中的讨论说:“对于有边框的形状(比如21),你可以分别给里面和外面上色。使用描边美学来修改边框的宽度"
  • @Axeman:好的,我们认为“讨论了一定长度”的内容有所不同。 aosmith 和 Gregor 给出的必要的scale_*guide 修改未在文档中明确提及。这些都是我正在寻找的答案的一部分。但是,嘿,没关系:这不是我的问题第一次被那些一开始就懒得理解这个问题的人投反对票。

标签: r ggplot2


【解决方案1】:

是的,你可以! ?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(带轮廓的三角形):

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 对应于轮廓。可以通过设置stroke来改变轮廓的权重。

如 cmets 中所述,您必须进行一些额外的调整才能使图例正确。添加到上面的情节:

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)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2015-03-11
    • 2022-12-24
    相关资源
    最近更新 更多