如何减少geom_text重叠 [英] How to reduce geom_text overlap

查看:580
本文介绍了如何减少geom_text重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集包含> 500个由各个运动员在不同位置进行的比赛活动的观察结果,并记录了整个足球比赛期间的情况.下面是我的数据集的一个示例,其中每个符号都表示一个匹配活动.例如,KE是踢脚有效的,记录在Defense中1分钟.

My dataset contains > 500 observations of match activities performed by individual athletes at different locations and recorded over the duration of a soccer match. An example of my dataset is below, where each symbol refers to a match activity. For example, KE is Kick Effective, recorded at 1 minute in the 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))

我希望通过绘制ggplot2中每个位置随时间变化的比赛活动来可视化此数据.

I wish to visualise this data, by plotting the match activities 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() 

但是,某些geom_text标签彼此重叠.我尝试过jitter,但是后来我失去了在足球场上进行活动的意义.不幸的是,check_overlap=TRUE删除了所有重叠的符号.我希望符号保持相同的文字方向.

However, some of the geom_text labels overlap one another. I have tried jitter but then I lose meaning of where the activity occurs on the soccer pitch. Unfortunately, check_overlap=TRUE removes any overlapped symbols. I wish to keep the symbols in the same text direction.

尽管符号是在符号出现时绘制的,但我很乐意稍微调整时间(请注意,它们将不再与图形完美对齐),以确保geom_text符号可见.我可以通过将每个重复出现的事件的Time向前或向后移动来手动完成此操作,但是对于如此大的数据集,这将需要很长时间.

Although the symbols are plotted at the time they occur, I am happy to adjust the time slightly (aware they will no longer perfectly align on the plot) to ensure the geom_text symbols are visible. I can do this manually by shifting the Time of each overlapped occurrence forward or back, but with such a big dataset this would take a very long time.

一个建议是使用ggrepel,尽管在​​y轴上更改了geom_text,但我还是在下面进行了此操作.

A suggestion was to use ggrepel and I did this below, although it alters the geom_text in the y-axis which is not what I am after.

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

有没有一种方法可以检查重叠并自动调整符号,以确保它们可见并仍在y轴上保留含义?也许一种解决方案是找到每个Location,并且如果Symbol与同一Location中的另一个Location相距不到两分钟,则调整Time.

Is there a way I can check for overlap and automatically adjust the symbols, to ensure they are visible and still retain meaning on the y-axis? Perhaps one solution could be to find each Location and if a Symbol is within two minutes of another in the same Location, Time is adjusted.

任何帮助将不胜感激.

推荐答案

我们可以添加点,然后对文本标签中的点使用最小线长的ggrepel.

We could add points, then use ggrepel with minimum line length to points from 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() 

或者,我们可以尝试将开发版本与方向" 参数.

Or we could try and use development version with "direction" argument.

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

这篇关于如何减少geom_text重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆