将标签与ggrepel对齐

Ada*_*m_G 6 r ggplot2 ggrepel

根据最新更新,ggrepel现在支持hjustvjust。根据文档,使用此选项应对齐所有标签。但是,我无法使标签对齐,如下所示

在此处输入图片说明

我试过了

library(tidyverse)
library(ggrepel)

df <- data.frame(x=seq(1:5), y=seq(1:5), label=letters[seq(1:5)])

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  hjust=0,
                  direction='y',
                  nudge_x=0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm')
Run Code Online (Sandbox Code Playgroud)

如何对齐这些标签?

编辑

我应该补充一点,这不仅是使标签对齐,而且还要使标签彼此靠近,并使用不同长度的连接器,以便于实现。

Cla*_*lke 7

首先,据我了解,这仅在开发版本中可用。因此,您需要从github安装它:

devtools::install_github("slowkow/ggrepel")
Run Code Online (Sandbox Code Playgroud)

其次,我认为这仅适用于具有相同x值(用于hjust)或y值(用于vjust)的数据点。

例:

library(tidyverse)
library(ggrepel)

df <- data.frame(x=seq(1:5), y=3, label=letters[seq(1:5)])

# not aligned
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  # vjust=0,
                  direction='y',
                  nudge_x=0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

# aligned bottom
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  vjust=0,
                  direction='y',
                  nudge_x=0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

# aligned top
ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  vjust=1,
                  direction='y',
                  nudge_x=0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


www*_*www 5

根据文档 ( https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html ),CRAN 上hjust的当前版本 ( 0.7.0)不支持。

此外,它看起来像你的directionnudge_xnudge_y不相关的。

我将您的代码稍微更改为以下三个版本。

direction = 'y'nudge_y = 0.1

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  direction = 'y',
                  nudge_y = 0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm') 
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

direction = 'x'nudge_x = 0.1

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  direction = 'x',
                  nudge_x = 0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

direction = 'both', nudge_x = 0.1, 和nudge_y = 0.3

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  direction = 'both',
                  nudge_x = 0.1,
                  nudge_y = 0.3,
                  segment.size=0.2) +
  geom_smooth(method='lm')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

它似乎正在工作。我唯一注意到的是,标签e似乎被限制,因为在限制xy-axis,所以你可能要进一步扩大轴如下。

ggplot(df, aes(x=x, y=y)) +
  geom_point() +
  geom_text_repel(aes(label=label),
                  force=1, point.padding=unit(1,'lines'),
                  direction = 'y',
                  nudge_y = 0.1,
                  segment.size=0.2) +
  geom_smooth(method='lm') +
  scale_y_continuous(limits = c(1, 5.5))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明