分面绘图注释增强版之annotation_custom2

2022-09-21 15:17:44 浏览数 (1)

❝本节来介绍一种新的方法来进行分面注释,下面通过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 ❞

0 人点赞