Alpha审美不适用于geom_segment - 为什么?

时间:2018-03-10 17:13:34

标签: r ggplot2 aesthetics

我一直在玩一些足球数据并且遇到了使用alpha美学的问题。基本上我试图在足球场上绘制一些传球:https://github.com/FCrSTATS/Visualisations/blob/master/3.CreateAPitch.md 然而,在使用geom_segment和alpha美学的同时,当alpha小于1时,我最终没有线条,只绘制了箭头。原因是什么?它是否具有在前面提到的链接中的函数中定义的主题? 我用过的代码:

pitch <- createPitch(10600, 7040, "#202020", "#797876", "#202020", "#131313")

pitch + geom_segment(data = data,
                 aes(x = x/100 * xmax, y = y/100 * ymax, 
                     xend = as.numeric(as.character(pass_end_x))/100 * xmax, 
                     yend = as.numeric(as.character(pass_end_y))/100 * ymax,
                     alpha = data$n/max(data$n)),
                 color = "blue", 
                 arrow = arrow(length = unit(0.3, "cm"), ends = 'first', type = 'closed'))

和数据:

structure(list(x = c(77.076, 66.7944444444444, 60.425, 64.7828571428571
), y = c(77.672, 19.0888888888889, 52.8055555555556, 90.0485714285714
), pass_end_x = c(88.296, 77.8240740740741, 70.0472222222222, 
73.5857142857143), pass_end_y = c(61.204, 20.7796296296296, 
64.0083333333333, 89.8485714285714), n = c(25L, 54L, 36L, 35L)), .Names = 
c("x", "y", "pass_end_x", "pass_end_y", "n"), row.names = c(NA, -4L), class 
= c("tbl_df", "tbl", "data.frame"))

1 个答案:

答案 0 :(得分:1)

忽略音高部分,摆脱$的效果很好:

dd = structure(list(x = c(77.076, 66.7944444444444, 60.425, 64.7828571428571
), y = c(77.672, 19.0888888888889, 52.8055555555556, 90.0485714285714
), pass_end_x = c(88.296, 77.8240740740741, 70.0472222222222, 
73.5857142857143), pass_end_y = c(61.204, 20.7796296296296, 
64.0083333333333, 89.8485714285714), n = c(25L, 54L, 36L, 35L)), .Names = 
c("x", "y", "pass_end_x", "pass_end_y", "n"), row.names = c(NA, -4L), class 
= c("tbl_df", "tbl", "data.frame"))

xmax = ymax = 1

p = ggplot(
    dd,
    aes(
        x = x / 100 * xmax,
        y = y / 100 * ymax,
        xend = as.numeric(as.character(pass_end_x)) / 100 * xmax,
        yend = as.numeric(as.character(pass_end_y)) / 100 * ymax,
        alpha = n / max(n)
    )
) +
    geom_segment(color = "blue",
                             arrow = arrow(
                                length = unit(0.3, "cm"),
                                ends = 'first',
                                type = 'closed'
                             ))

p

enter image description here

您可以轻松查看透明度差异。从链接添加主题仍然很好:

p + theme_blankPitch()

enter image description here

根据您的问题添加音调会因为您的颜色选择而难以看到线条,但它们仍然存在:

pitch <- createPitch(10600, 7040, "#202020", "#797876", "#202020", "#131313")
pitch + geom_segment(data = dd,
    aes(
        x = x / 100 * xmax,
        y = y / 100 * ymax,
        xend = as.numeric(as.character(pass_end_x)) / 100 * xmax,
        yend = as.numeric(as.character(pass_end_y)) / 100 * ymax,
        alpha = n / max(n)
    ), color = "blue",
                             arrow = arrow(
                                length = unit(0.3, "cm"),
                                ends = 'first',
                                type = 'closed'
                             ))

enter image description here

如果你增加片段的大小(比如size = 1.5),它们会更清晰,但我会建议不同的颜色选择。您可以进行的另一项调整是设置scale_alpha_continuous的限制,以使范围的最小值更加不透明。默认最小值为0.1,您可以将其提高到0.2或更高。