0

I have data from an athlete and the position they play, for every quarter of a match. Each quarter goes for a maximum of 20 minutes.

I wish to plot the time spent in each position, by this player, and change the colour according to the position. My question is similar to this one, so I used the geom_rect code in ggplot2.

An example of my data is:

# Sample data.frame
df <- data.frame(Time=c(0, 5.35, 19.26, 23.32, 
                            9.08, 13.11, 0, 0, 
                            7.36, 2.51, 6.44, 22.47,
                            0, 24.38, 11.36), 
                     Athlete = c('Paul', 'Paul', 'Paul', 'Paul',
                                 'Paul', 'Paul', 'Paul','Paul',
                                 'Paul', 'Paul', 'Paul','Paul',
                                 'Paul', 'Paul', 'Paul'),
                     Quarter = c('Q1', 'Q1', 'Q1', 'Q1', 
                                'Q2', 'Q2', 'Q2', 'Q3', 
                                'Q3', 'Q3', 'Q4', 'Q4', 
                                'Q4', 'Q4', 'Q4'),
                     Position = c('Bench','Defender','Bench','Defender',
                                 'Bench','Defender','Defender','Defender',
                                 'Defender','Bench','Bench','Bench', 
                                 'Defender', 'Defender', 'Defender'))

I add in their ranked value of 50 for the match and plot this over time. The code I use to do this is:

# Add in necessary column
df$Value <- c(50)
    # Plot
    ggplot(df, aes(x = Time, y = Value)) +
      geom_rect(aes(NULL, NULL, 
                    xmin=Time, xmax=30, 
                    ymin=0, ymax=50, 
                    fill = Position)) + 
      scale_y_continuous(expand = c(0, 0), limits = c(0, 50)) + 
      scale_x_continuous(expand = c(0, 0), limits = c(0, 30)) + 
      theme_classic() +
      theme(legend.position = "none", 
            axis.title.x = element_blank(), 
            axis.title.y = element_blank(), 
            axis.text.y = element_blank(),
            axis.text.x = element_text(face = "bold", colour = "black", size = 10),
            axis.ticks.y = element_blank(), 
            axis.line.y = element_blank(), 
            panel.spacing = unit(1.5, "lines"), 
            strip.text = element_text(color = "black", size = 12, face = "bold")) +
      facet_wrap(~ Quarter, nrow = 1)

However the plot doesn't show the correct colour when the athlete was benched during quarter 2 and 4. For example, Paul started as a defender for Q2, then went to the bench at 9.08 then played as a defender from the 13.11 minute mark. Rather than showing this segment of time per colour, Q2 is just all blue.

Am I entering the wrong code for geom_rect?

Community
  • 1
  • 1
user2716568
  • 1,866
  • 3
  • 23
  • 38
  • Your rectangles are overlaid because you set xmax=30. You can see it with alpha=0.5. – baptiste Mar 18 '17 at 05:42
  • Thanks, how do I set xmax to ensure I see this correctly? – user2716568 Mar 18 '17 at 05:43
  • there are too many things I don't understand about the data for me to answer. I assume it's some kind of football terminology, the meaning of Time and quarters etc. eludes me. – baptiste Mar 18 '17 at 05:48

1 Answers1

0

I could get the desired plot by simply re-ordering the data frame so that time was sequential. I did this using the following code, which created the desired plot:

# Order data.frame
attach(df)
df <- df[order(Athlete, Quarter, Time),]
# Re-plot
ggplot(df, aes(x = Time, y = Value)) +
  geom_rect(aes(NULL, NULL, 
                xmin=Time, xmax=30, 
                ymin=0, ymax=50, 
                fill = Position)) + 
  scale_y_continuous(expand = c(0, 0), limits = c(0, 50)) + 
  scale_x_continuous(expand = c(0, 0), limits = c(0, 30)) + 
  theme_classic() +
  theme(legend.position = "bottom", 
        axis.title.x = element_blank(), 
        axis.title.y = element_blank(), 
        axis.text.y = element_blank(),
        axis.text.x = element_text(face = "bold", colour = "black", size = 10),
        axis.ticks.y = element_blank(), 
        axis.line.y = element_blank(), 
        panel.spacing = unit(1.5, "lines"), 
        strip.text = element_text(color = "black", size = 12, face = "bold")) +
  facet_wrap(~ Quarter, nrow = 1)
user2716568
  • 1,866
  • 3
  • 23
  • 38