0

I've been playing around with some football data and came across the problem with alpha aesthetic use. Basically I tried to plot some passes on the football pitch created as here: https://github.com/FCrSTATS/Visualisations/blob/master/3.CreateAPitch.md However, while doing so with geom_segment and alpha aesthetic to I ended up without the lines when alpha was less than 1, only the arrows were plotted. What's the reason? Is it something with theme defined in a function from the link mentioned before? A code I have used:

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

and the data:

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"))
jakes
  • 1,964
  • 3
  • 18
  • 50

1 Answers1

1

Ignoring the pitch part, getting rid of the $ works just fine:

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

You can easily see the transparency differences. Adding the theme from your link is still fine:

p + theme_blankPitch()

enter image description here

Adding the pitch from your question makes the lines harder to see due to your color choices, but they are still there:

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

If you increase the size of the segments (say, size = 1.5) they are a little clearer, but I would recommend different color choices. Another adjustment you can make is to set the limits of scale_alpha_continuous to make the minimum of the range more opaque. The default minimum is 0.1, you could raise it to 0.2 or even higher.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Well, this is quite embarrassing, but what I have noticed now is that the lines were not displayed in the plot pane. After zooming out there were displayed ok. Anyway, thanks for the help! – jakes Mar 10 '18 at 20:11