用ggrepel包画图标记不重叠标签

来源:EasyCharts1 14,520

当我们在图形中添加标签时,标签之间很容易相互重叠,包ggrepel就专门用来解决这个问题! 首先我们来看看通过geom_text()添加标签时的情形:

  1. library(ggplot2)
  2. #使用数据集mtcars演示
  3. ggplot(mtcars)+ geom_point(aes(wt, mpg), color="red")+
  4. geom_text(aes(wt, mpg, label=rownames(mtcars)))+
  5. theme_classic(base_size = 16)

用ggrepel包画图标记不重叠标签-图片1

可以看到可视化效果不是很好。接下来看看包ggrepel的效果。
#geom_text_repel()

geom_text_repel()是基于geom_text()

  1. library(ggrepel)
  2. set.seed(42)
  3. ggplot(mtcars)+ geom_point(aes(wt, mpg), color="red")+
  4. geom_text_repel(aes(wt, mpg, label=rownames(mtcars)))+
  5. theme_classic(base_size = 16)

用ggrepel包画图标记不重叠标签-图片2

geom_label_repel()

geom_label_repel()是基于geom_label(),它将标签置于一个小方框中

  1. set.seed(42)
  2. ggplot(mtcars)+ geom_point(aes(wt, mpg), color="grey", size=5)+
  3. geom_label_repel(aes(wt, mpg, fill=factor(cyl),
  4. label=rownames(mtcars)), fontface="bold", color="white",
  5. box.padding=unit(0.35, "lines"), point.padding=unit(0.5, "lines"),
  6. segment.colour = "grey50")+ theme_classic(base_size = 16)

参数

大部分geom_text()的参数都适用于geom_text_repel(),除了以下几个:

  • hjust
  • vjust
  • position
  • check_overlap

ggrepel包为geom_text_repel()与geom_label_repel()提供了特有的参数设置:

  • segment.color:连接点与标签的线段的颜色
  • segment.size:线段的粗细
  • segment.alpha:线段的透明度
  • box.padding:文本框周边填充
  • point.padding:点周围填充
  • arrow:grid:arrow提供的箭头
  • force:强制性将重叠文本散开
  • max.oter:最大迭代次数
  • nudge_x/y:标签开始位置在坐标轴的移动距离
  • direction:允许标签的方向,x、y or both

下面举个栗子来详细了解这些参数的图形效果

  1. set.seed(42)
  2. ggplot(mtcars)+ geom_point(aes(wt, mpg, color=factor(cyl)), size=3)+
  3. geom_text_repel(aes(wt, mpg, color=factor(cyl),
  4. label=rownames(mtcars), angle=ifelse(mtcars$cyl==4, 90, 0)),
  5. size=4, family="Times", fontface="bold",
  6. box.padding=unit(0.5, "lines"), point.padding=unit(1.6, "lines"),
  7. segment.color = "#cccccc", segment.size = 0.5,
  8. arrow = arrow(length=unit(0.01, "npc")),force = 1, max.iter = 3e3,
  9. nudge_x = ifelse(mtcars$cyl==6, 2, 0), nudge_y = ifelse(mtcars$cyl==6, 9, 0))+
  10. scale_color_discrete(name="cyl")+
  11. scale_x_continuous(expand = c(0.5, 0))+
  12. scale_y_continuous(expand = c(0.25, 0))+
  13. theme_classic(base_size = 16)

用ggrepel包画图标记不重叠标签-图片3

也可以通过设置参数point.padding=NA不对点进行repel

  1. set.seed(42)
  2. mtcars$label <- rownames(mtcars)
  3. ggplot(mtcars, aes(wt, mpg, label=label))+
  4. geom_point(color="red")+ geom_text_repel(point.padding = NA)+
  5. theme_classic(base_size = 16)

用ggrepel包画图标记不重叠标签-图片4

通过赋值某些label空字符“”来隐藏。

  1. set.seed(42)
  2. mtcars$label <- rownames(mtcars)
  3. mtcars$label[1:15] <- ""
  4. ggplot(mtcars, aes(wt, mpg))+ geom_point(aes(color=factor(cyl)), size=2)+
  5. geom_text_repel(aes(color=factor(cyl), size=hp, label=label),
  6. point.padding = unit(0.25, "lines"), box.padding = unit(0.25, "lines"),
  7. nudge_y = 0.1)+ theme_classic(base_size = 16)

用ggrepel包画图标记不重叠标签-图片5

将标签控制在特定区域
通过设置参数xlim和ylim来限制label的位置

  1. set.seed(42)
  2. data <- mtcars
  3. mu <- mean(data$wt)
  4. left <- data[data$wt <mu, ]
  5. right <- data[data$wt>=mu, ]
  6. ggplot()+ geom_vline(xintercept = mu)+
  7. geom_point(aes(wt, mpg), data=data)+
  8. geom_text_repel(data=left, aes(wt, mpg, label=rownames(left),
  9. color="Left half"), xlim=c(NA, mu))+
  10. geom_text_repel(data=right, aes(wt, mpg, label=rownames(right),
  11. color="Rigth half"), xlim=c(mu, NA))+ theme_classic(base_size = 16)

用ggrepel包画图标记不重叠标签-图片6
通过控制参数direction来决定label是左右移动还是上下移动, 默认是both

  1. set.seed(42)
  2. #direction="x" 左右移动
  3. ggplot(mtcars)+
  4.  geom_point(aes(wt, mpg), color="red")+
  5.  geom_text_repel(aes(wt, mpg, label=rownames(mtcars)), direction="x")+
  6.  theme_classic(base_size = 16)+xlim(1, 6)

用ggrepel包画图标记不重叠标签-图片7

  1. #direction="x" 上下移动
  2. ggplot(mtcars)+
  3.  geom_point(aes(wt, mpg), color="red")+
  4.  geom_text_repel(aes(wt, mpg, label=rownames(mtcars)), direction="y")+
  5.  theme_classic(base_size = 16)+xlim(1, 6)

用ggrepel包画图标记不重叠标签-图片8

线图

  1. set.seed(42)
  2. ggplot(Orange, aes(age, circumference, color=Tree))+
  3.  geom_line()+
  4.  coord_cartesian(xlim=c(min(Orange$age), max(Orange$age)+90))+
  5.  geom_text_repel(data=subset(Orange, age==max(age)),
  6.                  aes(label=paste("Tree", Tree)),size=6, nudge_x = 45, segment.color = NA)+
  7.  theme_classic(base_size = 16)+
  8.  theme(legend.position = "none")+
  9.  labs(title="Orange Trees", x="Age(days)", y="Circumference(mm)")

用ggrepel包画图标记不重叠标签-图片9

SessionInfo

  1. sessionInfo()
  2. ## R version 3.4.0 (2017-04-21)
  3. ## Platform: x86_64-w64-mingw32/x64 (64-bit)
  4. ## Running under: Windows 8.1 x64 (build 9600)
  5. ##
  6. ## Matrix products: default
  7. ##
  8. ## locale:
  9. ## [1] LC_COLLATE=Chinese (Simplified)_China.936
  10. ## [2] LC_CTYPE=Chinese (Simplified)_China.936
  11. ## [3] LC_MONETARY=Chinese (Simplified)_China.936
  12. ## [4] LC_NUMERIC=C
  13. ## [5] LC_TIME=Chinese (Simplified)_China.936
  14. ##
  15. ## attached base packages:
  16. ## [1] stats graphics grDevices utils datasets methods base
  17. ##
  18. ## other attached packages:
  19. ## [1] ggrepel_0.6.5 ggplot2_2.2.1
  20. ##
  21. ## loaded via a namespace (and not attached):
  22. ## [1] Rcpp_0.12.11 digest_0.6.12 rprojroot_1.2 plyr_1.8.4
  23. ## [5] grid_3.4.0 gtable_0.2.0 backports_1.1.0 magrittr_1.5
  24. ## [9] evaluate_0.10 scales_0.4.1 rlang_0.1.1 stringi_1.1.5
  25. ## [13] lazyeval_0.2.0 rmarkdown_1.5 labeling_0.3 tools_3.4.0
  26. ## [17] stringr_1.2.0 munsell_0.4.3 yaml_2.1.14 compiler_3.4.0
  27. ## [21] colorspace_1.3-2 htmltools_0.3.6 knitr_1.16 tibble_1.3.3

    • 猫鬼修 0

      你好~请问怎么修改标签小方框的颜色

    发表评论

    匿名网友

    拖动滑块以完成验证
    加载失败