【包】ggrepel

2023-02-27  本文已影响0人  JamesMori

基本功能

参数

参数
图形属性1
图形属性2

example

1.给名称赋‘’(空值)来控制显示哪些labels
2.显示所有labels

options(ggrepel.max.overlaps = Inf)
geom_text_repel(max.overlaps)#可覆盖上面的全局设置

3.label与点不分离,但是lebels之间以及labels与边框仍然会分离

geom_text_repel(point.size = NA)

4.label与边界不分离,但超过边界的部分会被覆盖

geom_text_repel(
    # Repel away from the left edge, not from the right.
    xlim = c(NA, Inf),
    # Do not repel from top or bottom edges.
    ylim = c(-Inf, Inf)
  )

5.设置边界不切割

coord_cartesian(clip = "off")

6.摆放labels


set.seed(42)
d <- data.frame(
  x1 = 1,
  y1 = rnorm(10),
  x2 = 2,
  y2 = rnorm(10),
  lab = state.name[1:10]
)

p <- ggplot(d, aes(x1, y1, xend = x2, yend = y2, label = lab, col = lab)) +
  geom_segment(size = 1) +
  guides(color = "none") +
  theme(axis.title.x = element_blank()) +
  geom_text_repel(
    nudge_x = -0.2, direction = "y", hjust = "right"
  ) +
  geom_text_repel(
    aes(x2, y2), nudge_x = 0.1, direction = "y", hjust = "left"
  )
摆放labels

扩展边界

p + scale_x_continuous(
  breaks = 1:2, labels = c("Dimension 1", "Dimension 2"),
  expand = expansion(mult = 0.5)
)
扩展边界

7.min.segment.length = 0画出所有线标,min.segment.length = Inf不画任何线标

p1 <- p +
  geom_text_repel(min.segment.length = 0, seed = 42, box.padding = 0.5) +
  labs(title = "min.segment.length = 0")

p2 <- p +
  geom_text_repel(min.segment.length = Inf, seed = 42, box.padding = 0.5) +
  labs(title = "min.segment.length = Inf")

gridExtra::grid.arrange(p1, p2, ncol = 2)
线标画不画

8.线标样式
segment.curvature = 1控制弧度,接近0则为尖角
segment.ncp = 3
segment.angle = 20扭曲角度
segment.square=F斜线,否则为弯曲线
segment.inflect=T引入拐点
segment.shape=-1~1线标间距离
segment.linetype线标类型


线

arrow = arrow(length = unit(0.015, "npc")转为画箭头


箭头
set.seed(42)
cars <- c("Volvo 142E", "Merc 230")

ggplot(dat, aes(wt, mpg, label = ifelse(car %in% cars, car, ""))) +
  geom_point(color = "red") +
  geom_text_repel(
    point.padding = 0.2, 
    nudge_x = .15,
    nudge_y = .5,
    segment.linetype = 6,
    segment.curvature = -1e-20,
    arrow = arrow(length = unit(0.015, "npc"))
  )
示例

9.为不一样大小的点注释

aes(point.size = cyl)

10.xlim与ylim限制labels的位置

x_limits <- c(3, NA)
#xlim  = x_limits

11.hjust调整对齐方式,direction控制排布方向,nudge调整字符开始位置与点的距离

set.seed(42)

dat <- subset(mtcars, wt > 2.75 & wt < 3.45)
dat$car <- rownames(dat)

ggplot(dat, aes(wt, mpg, label = car)) +
  geom_text_repel(
    data          = subset(dat, wt > 3),
    nudge_x       = 3.5 - subset(dat, wt > 3)$wt,
    segment.size  = 0.2,
    segment.color = "grey50",
    direction     = "y",
    hjust         = 0
  ) +
  geom_text_repel(
    data          = subset(dat, wt < 3),
    nudge_x       = 2.7 - subset(dat, wt < 3)$wt,
    segment.size  = 0.2,
    segment.color = "grey50",
    direction     = "y",
    hjust         = 1
  ) +
  scale_x_continuous(
    breaks = c(2.5, 2.75, 3, 3.25, 3.5),
    limits = c(2.4, 3.8)
  ) +
  geom_point(color = "red")
调整
  1. position with jitter or...
mtcars$label <- rownames(mtcars)
mtcars$label[mtcars$cyl != 6] <- ""

# New! (not available in ggplot2 version 2.2.1 or earlier)
pos <- position_jitter(width = 0.3, seed = 2)

ggplot(mtcars, aes(factor(cyl), mpg, color = label != "", label = label)) +
  geom_point(position = pos) +
  geom_text_repel(position = pos) +
  theme(legend.position = "none") +
  labs(title = "position_jitter()")
jitter
mtcars$label <- rownames(mtcars)
mtcars$label[mtcars$cyl != 6] <- ""

library(ggbeeswarm)
pos <- position_quasirandom()

ggplot(mtcars, aes(factor(cyl), mpg, color = label != "", label = label)) +
  geom_point(position = pos) +
  geom_text_repel(position = pos) +
  theme(legend.position = "none") +
  labs(title = "position_quasirandom()")
quasirandom

13.verbose = TRUE输出提示信息

上一篇下一篇

猜你喜欢

热点阅读