使用geom_text_repel保留顺序

时间:2018-06-25 13:29:07

标签: r ggplot2 ggrepel

我正在尝试为浮点数绘制一条数字线(略有简化,带有符号位,系数一位(当不为零时),而两位为指数)。我有这个:

library(ggplot2)
library(tibble)
library(ggrepel)

d <- tribble(
  ~repr,             ~number,
 "1.1[2] %*% 2^-11", 2**-3 + 2**-4,
 "1.0[2] %*% 2^-11", 2**-3,
 "1.1[2] %*% 2^-10", 2**-2 + 2**-3,
 "1.0[2] %*% 2^-10", 2**-2,
 "1.1[2] %*% 2^-01", 2**-1 + 2**-2,
 "1.0[2] %*% 2^-01", 2**-1,
 "1.0[2] %*% 2^0",   1,
 "1.1[2] %*% 2^0",   1 + 2**-1,
 "0.0[2] %*% 2^0",   0,
 "1.1[2] %*% 2^01",   2 + 1,
 "1.0[2] %*% 2^01",   2,
 "1.1[2] %*% 2^10",   2**2 + 2**1,
 "1.0[2] %*% 2^10",   2**2,
 "1.1[2] %*% 2^11",   2**3 + 2**2,
 "1.0[2] %*% 2^11",   2**3,

 "-1.1[2] %*% 2^-11", -(2**-3 + 2**-4),
 "-1.0[2] %*% 2^-11", -(2**-3),
 "-1.1[2] %*% 2^-10", -(2**-2 + 2**-3),
 "-1.0[2] %*% 2^-10", -(2**-2),
 "-1.1[2] %*% 2^-01", -(2**-1 + 2**-2),
 "-1.0[2] %*% 2^-01", -(2**-1),
 "-1.0[2] %*% 2^0",   -1,
 "-1.1[2] %*% 2^0",   -(1 + 2**-1),
 "-1.1[2] %*% 2^01",  -(2 + 1),
 "-1.0[2] %*% 2^01",  -2,
 "-1.1[2] %*% 2^10",  -(2**2 + 2**1),
 "-1.0[2] %*% 2^10",  -2**2,
 "-1.1[2] %*% 2^11",  -(2**3 + 2**2),
 "-1.0[2] %*% 2^11",  -2**3
)

ggplot(d) +
  geom_text_repel(aes(x = number, y = -0.1, label = number),
                  parse = TRUE,
                  angle = 0,
                  ylim = c(NA, -0.1)) +
  geom_text_repel(aes(x = number, y = 0.1, label = repr),
                  angle = 0,
                  parse = TRUE, direction = "both", #angle = 90,
                  ylim = c(0.1, NA)) +
  geom_hline(yintercept = 0) +
  coord_cartesian(ylim = c(-2, 2)) +
  labs(x = NULL, y = NULL) +
  theme_classic() +
  theme(axis.line.y = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank())

结果如下:

Number line plot

这几乎是我想要的,但是我希望标签按照与数字相同的顺序从左到右排序(并且我希望避免越过任何行)。

有没有一种方法可以避免与geom_text_repel越界?我不介意标签在零附近有较大的分布,那里有很多值,我只是觉得很难按原样阅读图表,因为标签按照它们的排列顺序被打乱了。 >

1 个答案:

答案 0 :(得分:1)

让我们添加另一列以放置文本标签:

d <- d[order(d$number),]
d$i <- seq(min(d$number), max(d$number), length.out = nrow(d))
head(d)

这里是:

            repr  number          i
-1.1[2] %*% 2^11     -12  -12.00000
-1.0[2] %*% 2^11      -8  -11.14285
-1.1[2] %*% 2^10      -6  -10.28571
-1.0[2] %*% 2^10      -4  -9.428571
-1.1[2] %*% 2^01      -3  -8.571429
-1.0[2] %*% 2^01      -2  -7.714286

我们不需要格雷格雷:

ggplot() +
geom_point(
  data = d,
  mapping = aes(x = 0, y = number),
  size = 0.1
) +
geom_text(
  data = d,
  mapping = aes(x = 0.05, y = i, label = repr),
  parse = TRUE, hjust = 0
) +
geom_segment(
  data = d,
  mapping = aes(x = 0, xend = 0.05, y = number, yend = i),
  size = 0.1
) +
scale_y_continuous(
  breaks = c(-12, -8, -6, -4, -3, -2, -1, 0, 1, 2, 3, 4, 6, 8, 12)
) +
coord_cartesian(xlim = c(-0.01, 0.12)) +
theme_classic(base_size = 14) +
labs(x = NULL, y = NULL) +
theme(
  axis.line.y = element_line(size = 0.2),
  axis.ticks.y = element_line(size = 0.2),
  axis.line.x = element_blank(),
  axis.ticks.x = element_blank(),
  axis.text.x = element_blank()
)

enter image description here