前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >分面绘图注释增强版之annotation_custom2

分面绘图注释增强版之annotation_custom2

作者头像
R语言数据分析指南
发布2022-09-21 15:17:44
4750
发布2022-09-21 15:17:44
举报
文章被收录于专栏:R语言数据分析指南

❝本节来介绍一种新的方法来进行分面注释,下面通过1个案例来进行展示

加载R包

代码语言:javascript
复制
library(tidyverse)
library(ggh4x)
library(ggsignif)
library(ggsci)
library(grid)

定义函数

代码语言:javascript
复制
annotation_custom2 <- function (grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, data) 
{
  layer(data = data, stat = StatIdentity, position = PositionIdentity, 
        geom = ggplot2:::GeomCustomAnn,
        inherit.aes = TRUE, params = list(grob = grob, 
                                          xmin = xmin, xmax = xmax, 
                                          ymin = ymin, ymax = ymax))
}

定义文本

代码语言:javascript
复制
grob <- grobTree(textGrob("Scatter plot", x=0.1,  y=0.95, hjust=0,
                          gp=gpar(col="red", fontsize=13, fontface="italic")))

### 定义线段数据
data.segm<-data.frame(x=5,xend=6,y=3,yend=3,name="Sepal.Width")

plot <- ggplot()+ geom_segment(data=data.segm,color="red",
             aes(x=x,y=y,yend=yend,xend=xend),inherit.aes=FALSE)+theme_void()

单分面注释

代码语言:javascript
复制
mtcars%>% ggplot(aes(mpg,disp))+
  geom_point()+facet_grid(vs~am)+
  annotation_custom2(grob,data = mtcars %>% filter(vs == 1,am==1))

❝看到此处,那么可以联想到使用「annotation_custom2」函数还可以跨分面添加注释信息,下面来通过一个例子来进行展示,当然细节很多,需要各位观众老爷细细体会 ❞

跨分面注释

代码语言:javascript
复制
p1 <- iris %>% pivot_longer(-Species) %>% 
  mutate(type=name) %>% 
  separate(type,into=c("type","type2",sep=".")) %>% 
  select(-.,-type2) %>% 
  ggplot(aes(Species,value,fill=Species))+
  stat_boxplot(geom="errorbar",
               position=position_dodge(width=0.8),width=0.2)+
  geom_boxplot(position=position_dodge(width =0.8))

pal <- c("#E64B35FF","#4DBBD5FF","#00A087FF","#F39B7FFF","#3C5488FF","#91D1C2FF")

### 构建线段数据
data.segm<-data.frame(x=0.5,xend=2,y=1,yend=1)

### 绘制线段
plot2 <- ggplot()+ geom_segment(data=data.segm,color="black",
                               aes(x=x,y=y,yend=yend,xend=xend),inherit.aes=FALSE)+
  theme_void()

### 构建显著性数据
grob2 <- grobTree(textGrob("***", x=1.8,  y=2.1,hjust=0,
                          gp=gpar(col="red", fontsize=18)))

数据可视化

代码语言:javascript
复制
 p1 + facet_nested(.~type+name,drop=T,scale="free",space="free",switch="y",
                  strip =strip_nested(background_x =elem_list_rect(fill =pal),by_layer_x = F))+
  theme(panel.spacing.x = unit(0, "cm"))+
  annotation_custom2(grob=ggplotGrob(plot2),data =iris %>% pivot_longer(-Species) %>% 
                       mutate(type=name) %>% 
                       separate(type,into=c("type","type2",sep=".")) %>% 
                       select(-.,-type2) %>% filter(name=="Petal.Length"),
                     ymin =2, ymax=2, xmin=0.8, xmax=Inf)+
   annotation_custom2(grob=ggplotGrob(plot2),data =iris %>% pivot_longer(-Species) %>% 
                        mutate(type=name) %>% 
                        separate(type,into=c("type","type2",sep=".")) %>% 
                        select(-.,-type2) %>% filter(name=="Petal.Width"),
                      ymin =2, ymax=2, xmin=0, xmax=1.1)+
  annotation_custom2(grob=grob2,data =iris %>% pivot_longer(-Species) %>% 
                       mutate(type=name) %>% 
                       separate(type,into=c("type","type2",sep=".")) %>% 
                       select(-.,-type2) %>% filter(name=="Petal.Length"),
                     ymin =2.1, ymax=2.1, xmin=1.8, xmax=2)+
   annotation_custom2(grob=ggplotGrob(plot2),data =iris %>% pivot_longer(-Species) %>% 
                        mutate(type=name) %>% 
                        separate(type,into=c("type","type2",sep=".")) %>% 
                        select(-.,-type2) %>% filter(name=="Petal.Length"),
                      ymin =8, ymax=8, xmin=Inf,xmax=Inf)+
   annotation_custom2(grob=ggplotGrob(plot2),data =iris %>% pivot_longer(-Species) %>% 
                        mutate(type=name) %>% 
                        separate(type,into=c("type","type2",sep=".")) %>% 
                        select(-.,-type2) %>% filter(name=="Petal.Width"),
                      ymin =8, ymax=8, xmin=-0.8,xmax=Inf)+
   annotation_custom2(grob=ggplotGrob(plot2),data =iris %>% pivot_longer(-Species) %>% 
                        mutate(type=name) %>% 
                        separate(type,into=c("type","type2",sep=".")) %>% 
                        select(-.,-type2) %>% filter(name=="Sepal.Length"),
                      ymin =8, ymax=8, xmin=-0.8,xmax=3.3)+
   annotation_custom2(grob=grob2,data =iris %>% pivot_longer(-Species) %>% 
                        mutate(type=name) %>% 
                        separate(type,into=c("type","type2",sep=".")) %>% 
                        select(-.,-type2) %>% filter(name=="Petal.Width"),
                      ymin =8.05, ymax=8.05, xmin=1.8, xmax=2)+
   
  labs(x=NULL,y=NULL)+scale_fill_brewer()+
  theme_minimal()+
  theme(axis.text.x = element_blank(),
        panel.spacing.x = unit(0, "cm"),
        legend.position = "npn")

❝使用「annotation_custom2」函数进行跨分面添加注释相对于之前的强行画图的确好很多,但是若需要注释的数据过多也比较繁琐,此外还有不少细节需要优化;那么本节介绍到此结束,「喜欢的观众老爷欢迎分享转发,每天都想着呈现一些实用内容给各位」,本文数据均使用R内置数据集, 参考链接如下https://rdrr.io/github/LoiseauN/dimensionality/src/R/annotation_custom2.R ❞

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-07-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 R语言数据分析指南 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加载R包
  • 定义函数
  • 定义文本
  • 单分面注释
  • 跨分面注释
  • 数据可视化
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档