How to reduce the overlap of geom_text

My dataset contains> 500 match observations made by individual athletes at different locations and recorded throughout the entire match. Below is an example of my dataset where each character refers to a match activity. For example KE

- Kick Effective recorded with 1 minute in Defense

.

# Example data
df <- data.frame(Symbol = c('KE', 'TE', 'TE', 'TI',
                              'KE', 'KE', 'H', 'H',
                              'GS', 'KE', 'TE', 'H',
                              'KE', 'H', 'H', 'GS'),
                Location = c('Defense', 'Defense', 'Midfield', 'Forward',
                             'Forward', 'Midfield', 'Midfield', 'Defense',
                             'Defense', 'Defense', 'Forward', 'Midfield',
                             'Midfield', 'Defense', 'Defense', 'Midfield'),
                 Time = c(1, 2, 3, 6,
                            15, 16, 16, 20,
                            22, 23, 26, 26,
                            27, 28, 28, 30))

      

I want to visualize this data by plotting appropriate actions over time at each location in ggplot2

.

# Load required package
require(ggplot2)
# Order factors for plotting
df$Location <- factor(df$Location, levels = c("Defense", "Midfield", "Forward"))

    # Plot
    ggplot(df, x = Time, y = Location) +
      geom_text(data=df, 
                aes(x = Time, y = Location, 
                    label = Symbol), size = 4) +
      theme_classic() 

      

However, some of the labels geom_text

overlap. I tried it jitter

, but then I lose sight of where the action takes place on the pitch. Unfortunately, it check_overlap=TRUE

removes any overlapping characters. I want to keep characters in one direction of the text.

Even though the symbols are displayed at the time they appear, I would happily adjust the timing a little (realizing that they will no longer be completely aligned on the graph) so that the symbols are visible geom_text

. I can do this manually by moving Time

each overlapping occurrence forward or backward, but with such a large dataset, it will take a very long time.

It was suggested to use ggrepel

and I did it below, although it changes geom_text

on the y-axis, which is not what I am doing.

library(ggrepel)
ggplot(df, x = Time, y = Location) +
  geom_text_repel(aes(Time, Location, label = Symbol)) 

      

Is there a way to check for a match and automatically adjust the symbols to be visible and keep the y-axis value? Perhaps one solution could be to find each Location

, and if a Symbol

is within two minutes of the other in the same Location

, it will Time

adjust.

Any help would be greatly appreciated.

+3


source to share


1 answer


We could add points and then use ggrepel

a minimum line length to the points from the text labels.

library(ggrepel) # ggrepel_0.6.5 ggplot2_2.2.1

ggplot(df, aes(x = Time, y = Location, label = Symbol)) +
  geom_point() +
  geom_text_repel(size = 4, min.segment.length = unit(0.1, "lines")) +
  theme_classic() 

      



enter image description here Or we could try and use the design version with the "direction" argument .

ggplot(df, aes(x = Time, y = Location, label = Symbol)) +
  geom_text_repel(size = 4, direction = "x") +
  theme_classic() 

      

+2


source







All Articles