【问题标题】:Aesthethics do not work for individual geom_line's in R's ggplot美学不适用于 R ggplot 中的单个 geom_line
【发布时间】:2021-08-23 04:04:59
【问题描述】:

我正在尝试为个人构建一个带有细而透明的单线的图(由 id 列指示),并在其上放置一条粗而实的跨人平均线。

我的 ggplot 调用如下所示:

ggplot(data, aes(x = card_self, y = percent)) +
  geom_line(aes(group=id, color=id, size=1, alpha=0.8)) + 
  geom_line(data = data_averaged, aes(size=10, alpha=0.9)) +
  guides(color='none', alpha='none', size='none')

结果是这样的:

问题是改变尺寸/alpha 美学效果没有按预期工作。我尝试将它们设置在 aes 参数之外和内部,结果是随机的。

我重现剧情的完整代码在这里:

library("tidyverse")
library("ggplot")
data <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                        1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                        3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
                                        4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                        5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
                                        7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 
                                        8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
                                        9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
                                        10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
                                        11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
                                        12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
                                        14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 
                                        15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 
                                        16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 
                                        17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 
                                        18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 
                                        19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 
                                        20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
                                        21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 
                                        22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 
                                        23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 
                                        24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 
                                        25L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 
                                        27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 28L, 
                                        28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 
                                        29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 30L, 30L, 30L, 
                                        30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L), .Label = c("P001", 
                                                                                                 "P002", "P003", "P004", "P005", "P006", "P007", "P008", "P009", 
                                                                                                 "P010", "P011", "P012", "P013", "P014", "P015", "P016", "P017", 
                                                                                                 "P018", "P019", "P020", "P021", "P022", "P023", "P024", "P025", 
                                                                                                 "P026", "P027", "P028", "P029", "P030"), class = "factor"), card_self = structure(c(1L, 
                                                                                                                                                                                     2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 
                                                                                                                                                                                     5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 
                                                                                                                                                                                     9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 
                                                                                                                                                                                     11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 
                                                                                                                                                                                     13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 
                                                                                                                                                                                     2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 
                                                                                                                                                                                     5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 
                                                                                                                                                                                     9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 
                                                                                                                                                                                     11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 
                                                                                                                                                                                     13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 
                                                                                                                                                                                     2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 
                                                                                                                                                                                     5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 
                                                                                                                                                                                     9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 
                                                                                                                                                                                     11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 
                                                                                                                                                                                     13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 
                                                                                                                                                                                     2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 
                                                                                                                                                                                     5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 
                                                                                                                                                                                     9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 
                                                                                                                                                                                     11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 
                                                                                                                                                                                     13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 
                                                                                                                                                                                     2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 
                                                                                                                                                                                     5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 
                                                                                                                                                                                     9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 
                                                                                                                                                                                     11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 
                                                                                                                                                                                     13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L), .Label = c("ace", 
                                                                                                                                                                                                                                                          "two", "three", "four", "five", "six", "seven", "eight", "nine", 
                                                                                                                                                                                                                                                          "ten", "jack", "queen", "king"), class = "factor"), percent = c(1, 
                                                                                                                                                                                                                                                                                                                          1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0.89, 1, 0.67, 0, 0, 
                                                                                                                                                                                                                                                                                                                          0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 
                                                                                                                                                                                                                                                                                                                          0.67, 0.22, 0.11, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0.89, 0, 0, 0, 
                                                                                                                                                                                                                                                                                                                          0, 0, 1, 1, 0.89, 1, 1, 0.89, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0.89, 
                                                                                                                                                                                                                                                                                                                          0.56, 0, 0.22, 0, 0, 0, 0, 0.56, 0.89, 0.67, 0.89, 0.67, 0.89, 
                                                                                                                                                                                                                                                                                                                          0.56, 0.67, 0.44, 0.89, 0.56, 0.56, 1, 1, 1, 1, 0.44, 0.56, 0.11, 
                                                                                                                                                                                                                                                                                                                          0.22, 0.22, 0, 0, 0, 1, 1, 1, 1, 0.44, 0.33, 0.67, 0.33, 0.11, 
                                                                                                                                                                                                                                                                                                                          0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0.89, 1, 0.89, 
                                                                                                                                                                                                                                                                                                                          0.78, 0.22, 0.56, 0.33, 0.22, 0.11, 0, 1, 1, 1, 1, 0.44, 0.11, 
                                                                                                                                                                                                                                                                                                                          0, 0, 0, 0, 0, 0, 1, 0.78, 0.89, 0.56, 0.67, 0.67, 0.44, 0.44, 
                                                                                                                                                                                                                                                                                                                          0.44, 0.67, 0.22, 0, 1, 1, 1, 1, 1, 0.78, 0.44, 0, 0.78, 0.44, 
                                                                                                                                                                                                                                                                                                                          0, 0, 1, 1, 1, 1, 1, 1, 0.11, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 
                                                                                                                                                                                                                                                                                                                          0, 0.22, 0, 0, 0, 0, 0.67, 0.44, 0.33, 0.56, 0.11, 0.33, 0.44, 
                                                                                                                                                                                                                                                                                                                          0.56, 0.44, 0.33, 0.22, 0.33, 1, 1, 1, 1, 1, 1, 0.11, 0.11, 0.11, 
                                                                                                                                                                                                                                                                                                                          0, 0, 0, 1, 1, 1, 1, 1, 0.67, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 
                                                                                                                                                                                                                                                                                                                          0.44, 0, 0.33, 0.22, 0, 0, 0, 1, 0.56, 0.67, 0.89, 0.89, 0.78, 
                                                                                                                                                                                                                                                                                                                          0.67, 0.44, 0.33, 0, 0.33, 0, 1, 1, 0.89, 0.78, 0.67, 0.44, 0.33, 
                                                                                                                                                                                                                                                                                                                          0, 0.22, 0, 0, 0, 1, 1, 1, 1, 1, 0.78, 0, 0, 0, 0, 0, 0, 1, 1, 
                                                                                                                                                                                                                                                                                                                          1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0.67, 0.44, 0.56, 0.22, 0.33, 0.44, 
                                                                                                                                                                                                                                                                                                                          0.56, 0.56, 0.22, 0.44, 0.44, 0.33, 1, 1, 1, 1, 1, 1, 0.56, 0.11, 
                                                                                                                                                                                                                                                                                                                          0, 0, 0, 0, 1, 1, 1, 1, 0.44, 0.44, 0.33, 0, 0, 0, 0, 0, 1, 1, 
                                                                                                                                                                                                                                                                                                                          0.89, 0.67, 0.33, 0.33, 0.56, 0.22, 0.11, 0.11, 0, 0, 1, 1, 1, 
                                                                                                                                                                                                                                                                                                                          1, 1, 0.78, 0.22, 0, 0, 0, 0, 0)), row.names = c(NA, -360L), groups = structure(list(
                                                                                                                                                                                                                                                                                                                            id = structure(1:30, .Label = c("P001", "P002", "P003", "P004", 
                                                                                                                                                                                                                                                                                                                                                            "P005", "P006", "P007", "P008", "P009", "P010", "P011", "P012", 
                                                                                                                                                                                                                                                                                                                                                            "P013", "P014", "P015", "P016", "P017", "P018", "P019", "P020", 
                                                                                                                                                                                                                                                                                                                                                            "P021", "P022", "P023", "P024", "P025", "P026", "P027", "P028", 
                                                                                                                                                                                                                                                                                                                                                            "P029", "P030"), class = "factor"), .rows = structure(list(
                                                                                                                                                                                                                                                                                                                                                              1:12, 13:24, 25:36, 37:48, 49:60, 61:72, 73:84, 85:96, 
                                                                                                                                                                                                                                                                                                                                                              97:108, 109:120, 121:132, 133:144, 145:156, 157:168, 
                                                                                                                                                                                                                                                                                                                                                              169:180, 181:192, 193:204, 205:216, 217:228, 229:240, 
                                                                                                                                                                                                                                                                                                                                                              241:252, 253:264, 265:276, 277:288, 289:300, 301:312, 
                                                                                                                                                                                                                                                                                                                                                              313:324, 325:336, 337:348, 349:360), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                                                                                                                                                                                                                                 "vctrs_vctr", "list"))), row.names = c(NA, -30L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "tbl_df", "tbl", "data.frame"))
data_averaged <- data %>%
  group_by(card_self) %>%
  summarise(percent = mean(percent))


ggplot(data, aes(x = card_self, y = percent)) +
  geom_line(aes(group=id, color=id, size=1, alpha=0.8)) + 
  geom_line(data = data_averaged, aes(size=10, alpha=0.9)) +
  guides(color='none', alpha='none', size='none')

【问题讨论】:

  • 您能否举例说明您所期望的特定行为与您所获得的行为?此外,个别情节(仅使用geom_line 语句之一)是否按预期工作?
  • @AllDownhillFromHere 现在有点晚了,但我刚刚回顾了这个,不得不指出你的评论很棒。这些都是我在发帖前应该思考和探索的问题,可惜我没有想到。很有见地!谢谢!

标签: r ggplot2


【解决方案1】:

更新:来自 teunbrand 的重要评论:

“此外,我想补充一点,应该添加组美学的原因是因为离散的 x 轴会自动对观察进行分组”

只需将group=1 添加到ggplot()

问题是第二个geom_line 没有分组。必须对数据点进行分组才能正确连接。

ggplot(data, aes(x = card_self, y = percent, group=1)) +
    geom_line(aes(group=id, color=id, size=1, alpha=0.8)) + 
    geom_line(data = data_averaged, aes(size=10, alpha=0.9)) +
    guides(color='none', alpha='none', size='none')


# also works:
ggplot(data, aes(x = card_self, y = percent)) +
    geom_line(aes(group=id, color=id, size=1, alpha=0.8)) + 
    geom_line(data = data_averaged, aes(size=10, alpha=0.9, group=1)) +
    guides(color='none', alpha='none', size='none')

【讨论】:

  • 另外,我想补充一点,应该添加组美学的原因是因为离散的x轴会自动对观察进行分组。
  • 非常感谢您的重要说明。我会将其添加为更新并参考您的评论。
  • 非常感谢。我猜第二条 geom 线继承了第一条线的组/颜色美学,所以我需要手动覆盖它们?但是,美学仍然存在一个问题:更改 alpha 值对绘图没有影响,例如ggplot(data, aes(x = card_self, y = percent)) + geom_line(aes(group=id, color=id, alpha=0.7)) + geom_line(data = data_averaged, aes(group=1, color='red ', size=3, alpha=0.8)) + guides(color='none', alpha='none', size='none') 更改 alpha 值(同时保持第一个较小)对绘图没有影响 - -有什么想法吗?
  • 你可以使用scale_alpha_manual(c(0.8, 0.9))
猜你喜欢
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多