【问题标题】:ggplot: customize the color of margins of dotsggplot:自定义点边距的颜色
【发布时间】:2019-03-28 17:32:25
【问题描述】:

我想自定义 ggplot 点的边缘颜色。点填充的颜色总结了一个信息,而 边距的颜色将总结另一个。

假设使用 mtcars 数据:

library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
    geom_point(aes(colour = factor(cyl), shape = factor(vs)))

然后假设您想使用列 mtcars$carb 修改点边距的颜色。

是否可以使用ggplot

【问题讨论】:

  • 你还需要映射一些东西来塑造吗?
  • 您需要具有单独填充和边框的形状,例如形状 21:25。所以你可以使用scale_shape_manual(values = c(21, 22, 23) ) 或其他东西,然后将fill 用于carb

标签: r ggplot2


【解决方案1】:

是的,您需要使用fill 作为内部颜色,使用color 作为线条颜色。您需要使用区分两者的形状(即形状 21-25。搜索“r pch”或查看帮助页面 ?pch 了解所有形状)。不幸的是,为了使图例看起来正确,我们需要手动指定图例的形状。您可能还想使用至少一个manual 比例(例如scale_fill_manual)来指定您自己的颜色,以便填充和线条颜色更加不同。

ggplot(mtcars,
    aes(
      x = wt,
      y = mpg,
      color = factor(carb),
      fill = factor(cyl),
      shape = factor(vs)
    )) + geom_point(size = 2, stroke = 1.5) +
  scale_shape_manual(values = c(21, 24)) +
  scale_fill_hue(guide = guide_legend(override.aes = list(shape = 21))) +
  scale_color_hue(guide = guide_legend(override.aes = list(shape = 21)))

另请参阅?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)

【讨论】:

  • 你知道填充图例全黑的原因吗?
  • 我通常最终不得不使用override.aes 来为fill 图例获得正确的“填充”形状。
  • @Bnf8 很高兴它有帮助。如果您认为问题已解决,请单击投票/分数旁边的复选标记“接受”答案。这表明您还没有在寻找更多答案(并给我们每个人一点名声)。
猜你喜欢
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
  • 2011-08-12
  • 1970-01-01
  • 2021-12-02
  • 2013-01-06
  • 1970-01-01
相关资源
最近更新 更多