【问题标题】:Place a border around different shape varieties in ggplot2 (R)在 ggplot2 (R) 中围绕不同形状品种放置边框
【发布时间】:2015-03-18 18:17:04
【问题描述】:

在尝试在ggplot2 中为geom_point()s 设置边框时,this question 是一个非常有用的起点。我最终选择了@joran(第二个答案)概述的方法,因为它允许我获得更小的轮廓(这是更可取的,因为这些点最终在最终产品中会非常小)。

但是,我在使用这种技术的细微变化时遇到了麻烦。我有两种不同的形状要勾勒出来,这给我带来了一些问题。这是一个可重现的示例:

require('ggplot2')

values <- rnorm(n = 10, mean = 1, sd = 0.5) + c(1:10)

df <- data.frame(id = rep(c('hq', 'lq'), each = 5),
                 values = values,
                 period = rep(c(1:5), 2))

plot <- 
    ggplot(df, aes(x = period,
                   y = values,
                   group = id)) +
    geom_line(color = 'gray40') +
    geom_point(aes(shape = id,
                   color = id),
               size = 3) +
    geom_point(aes(shape = id),
               color = 'gray70',
               size = 3,
               show_guide = FALSE) +
    scale_color_manual(values = c('lightskyblue1', 'lightpink'),
                       labels = c('HQ', 'LQ')) +
    scale_shape_manual(values = c(15, 16, 0, 1),
                       labels = c('HQ', 'LQ')) +
    theme_bw()

这最终给出了一个看起来像这样的情节:

我不明白为什么这些点会变成纯灰色(这是我希望蓝色和粉红色点周围的边框的颜色)。任何帮助保留与形状类型匹配的灰色边框的蓝色和粉红色填充将不胜感激!

编辑:如果不清楚,我知道我可以使用pch 21-25 来实现边框。但是,这些边框太大,它们的厚度不可修改(据我所知)。结果,我尝试将两个geom_points()s 分层,一个填充另一个未填充,以实现边框的外观。我可以通过像这样分层另一组geom_point()s 来实现这一点:

plot <- 
    ggplot(df, aes(x = period,
                   y = values,
                   group = id)) +
    geom_line(color = 'gray40') +
    geom_point(aes(shape = id,
                   color = id),
               size = 3) +
    geom_point(shape = 1,
               color = 'gray70',
               size = 3,
               show_guide = FALSE) +
    scale_color_manual(values = c('lightskyblue1', 'lightpink'),
                       labels = c('HQ', 'LQ')) +
    scale_shape_manual(values = c(15, 16),
                       labels = c('HQ', 'LQ')) +
    theme_bw()

这里的问题是每个“边框”都不匹配对应的形状,生成的图像是这样的:

【问题讨论】:

  • 只有点字符 (pch) 21 到 25 支持填充和颜色。您需要使用这些符号,并指定填充美学。
  • @MatthewPlourde 我知道我可以使用 pch 21 到 25,但是这些点周围的边界太大了,因为正如我所提到的,最终产品会有非常小的点。我想要做的是第二层 geom_point()s 来实现相同的效果——一个实心的,一个填充的,这样我就可以实现边框的外观。
  • 您需要在第一个geom_point 中也包含aes(shape = id),否则R 不知道灰点需要具有哪种形状。检查我编辑的答案

标签: r ggplot2


【解决方案1】:

其实你不用那么多aes的。 ggplot 中只有一个 aes 就足够了。 aes 将自动应用于另一个 geom_..,除非它被覆盖。

plot <- 
  ggplot(df, aes(x = period, y = values, group = id, shape = id, color=id)) +
  geom_line(color = 'gray40') +
  geom_point(color = 'gray70', size = 5, show_guide = FALSE) +
  geom_point(size = 3) +
  scale_color_manual(values = c('lightskyblue1', 'lightpink'), labels = c('HQ', 'LQ')) +
  scale_shape_manual(values = c(15, 16, 0, 1), labels = c('HQ', 'LQ')) +
  theme_bw()

【讨论】:

  • 非常感谢!你对清理aes 垃圾的看法是正确的——我正在掌握ggplot 的窍门,所以我很感激你的建议。
  • 其实你也可以删除group = id,因为colorshape会告诉组。
【解决方案2】:

已编辑

我看错了你的问题。无论如何,解决方案是按geom_point的顺序:

  1. 第一个在背景中,所以我用它来绘制更大的灰色形状(5 号)

    geom_point(aes(shape = id),
               color = 'gray70',
               size = 5,
               show_guide = FALSE)
    
  2. 第二个绘制彩色形状:

    geom_point(aes(shape = id,
                   color = id),
               size = 3)
    

这个:

plot <- 
  ggplot(df, aes(x = period,
                 y = values,
                 group = id)) +
  geom_line(color = 'gray40') +
  geom_point(aes(shape = id),
             color = 'gray70',
             size = 5,
             show_guide = FALSE) + 
  geom_point(aes(shape = id,
                 color = id),
             size = 3) +
  scale_color_manual(values = c('lightskyblue1', 'lightpink'),
                     labels = c('HQ', 'LQ')) +
  scale_shape_manual(values = c(15, 16, 0, 1),
                     labels = c('HQ', 'LQ')) +
  theme_bw()

使用这段代码我可以达到这个结果:

【讨论】:

  • 谢谢,geom_point()s 的订购确实是我遇到的问题。
【解决方案3】:

刚刚看到@mucio 更新了他的答案 - 我认为他的答案更优雅一些,尽管这种方式可能会给你想要的细边框。


由于您两次都使用id 作为形状图例,所以彩色形状和灰色形状都被填充了。解决这个问题的一种方法(诚然是一个 hack,但到目前为止这似乎并没有阻止你),是创建一个具有新级别的新变量并将其用于灰色形状中的形状。 (另请注意,我必须重新排列形状规范的顺序)。

df$id2 <- ifelse(df$id == 'hq', 'hq_alt', 'lq_alt')
plot <- 
    ggplot(df, aes(x = period,
                   y = values,
                   group = id)) +
    geom_line(color = 'gray40') +

     geom_point(aes(shape = id,
                   color = id),
               size = 3) +
    geom_point(aes(shape = id2),
               color = 'gray70',
               size = 3,
               show_guide = FALSE) +
    scale_color_manual(values = c('lightskyblue1', 'lightpink'),
                       labels = c('HQ', 'LQ')) +
    scale_shape_manual(values = c(15, 0, 16, 1),
                       labels = c('HQ', 'LQ'), guide=F) +
    theme_bw()

【讨论】:

  • 哈哈,你说得对,我现在不介意黑客攻击——只是暂时尝试完成它。创造性的解决方法,但你是对的,不像 @mucio 的解决方案那样优雅。
猜你喜欢
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-12
相关资源
最近更新 更多