查看原文
其他

R语言可视化学习笔记之ggrepel包

2017-06-14 TaoYan EasyCharts

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

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

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

geom_text_repel()是基于geom_text()

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

geom_label_repel()

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

set.seed(42) ggplot(mtcars)+ geom_point(aes(wt, mpg), color="grey", size=5)+ geom_label_repel(aes(wt, mpg, fill=factor(cyl), label=rownames(mtcars)), fontface="bold", color="white", box.padding=unit(0.35, "lines"), point.padding=unit(0.5, "lines"), 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

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

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

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

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

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

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

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

set.seed(42) data <- mtcars mu <- mean(data$wt) left <- data[data$wt <mu, ] right <- data[data$wt>=mu, ] ggplot()+ geom_vline(xintercept = mu)+ geom_point(aes(wt, mpg), data=data)+ geom_text_repel(data=left, aes(wt, mpg, label=rownames(left), color="Left half"), xlim=c(NA, mu))+ geom_text_repel(data=right, aes(wt, mpg, label=rownames(right), color="Rigth half"), xlim=c(mu, NA))+ theme_classic(base_size = 16)


通过控制参数direction来决定label是左右移动还是上下移动, 默认是both

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

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

线图

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

SessionInfo

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

【重磅】史上最全的论文图表基本规范

关于学术论文Figures,你不能不知道的秘密

优雅的操纵json数据地图素材——打破地理信息可视化的孤岛

用R-Shiny打造一个美美的在线App

shiny动态仪表盘——360度全空间无死角拖拉换肤功能的旋转地球


如需转载请联系EasyCharts团队!


送福利啦!

 长按文末二维码,关注EasyCharts公众号;

 加入QQ群,可以在群文件获取“Excel商业图表修炼秘笈之基础篇-大纲”;

 加入QQ群,可以在群文件获取《Excel数据之美》试读样章、与书籍配套的Excel源文件和EasyCharts插件;


在公众号中回复“买书”,即可收到购买链接哦!


我们EasyCharts团队在网易云课堂等你来上课哦,长按下面的二维码就可进入网易学习课堂。本课程定价为19.9


简介

真正的高手,不是会制作高难度的图表,而是能把最平常的图表绘制出商务范!小菜鸟,老司机,都不容错过本课程哦!

1. 这是Excel史上最全的基础图表类型讲解;本课程重点讲解了23个基础图表案例,包括Excel自动生成的柱形图,条形图,折线图,面积图,散点图,饼图,圆环图、雷达图,表格,卡片等常用商业图表。

2. 这是Excel史上最全的商业图表风格学习大全;本课程提供了5种商业图表风格的图表,包括《商业周刊》风格,《华尔街日报》风格1和2,《经济学人》风格1和2,共5种经典商业杂志图表的风格。

3. 本课程包括了150多张张商业图表演示Excel源文件。每个基础图表案例至少提供6种以上不同的图表风格演示,供读者选择与学习,可以满足不同人群的审美需求。

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存