【R>>aplot】装扮你的ggplot2
2021-07-06 本文已影响0人
高大石头
科研搬砖日常,往往需要将各种图进行拼接,常用的组合包有patchwork
、cowplot
和gridExtra
等。但是这几款都是类似将图片进行拼接,图与图之间的关系是平等的,尽管可通过调整参数突出主图,但参数相对较多,不好记忆;而aplot正好能解决对主图注释这一痛点。下面就来学习下吧。
核心函数:
-
insert_left()
insert_right()
insert_top()
insert_bottom()
-
is.ggtree()
-
plot_list()
-
xlim2()
ylim2()
-
xrange()
yrange()
1.核心函数
1.1 insert_**()
rm(list = ls())
library(ggplot2)
library(aplot)
p <- ggplot(mtcars,aes(mpg,disp))+
geom_point()
p2 <- ggplot(mtcars,aes(mpg))+
geom_density(fill="steelblue",alpha=0.5)+
ggtree::theme_dendrogram()
p3 <- ggplot(mtcars,aes(x=1,y=disp))+
geom_boxplot(fill="firebrick",alpha=0.5)+
theme_void()
p+p2+p3
ap <- p %>%
insert_top(p2,height = 0.3) %>%
insert_right(p3,width = 0.1)
ap
1.2 xlim2
p1 <- ggplot(mtcars,aes(cyl))+
geom_bar()
p2 <- ggplot(subset(mtcars,cyl!=4),aes(cyl))+
geom_bar()
p1+p2
p2+xlim2(p1)
2.实战一:heatmap
rm(list = ls())
pacman::p_load(tidyverse,ggtree,aplot,reshape,ggExtra)
d <- matrix(rnorm(25),ncol = 5) %>%
data.frame()
rownames(d) <- paste0("g",1:5)
colnames(d) <- paste0("t",1:5)
hc <- hclust(dist(d))
hcc <- hclust(dist(t(d)))
phr <- ggtree(hc) #行聚类树
phc <- ggtree(hcc)+layout_dendrogram() #列聚类树
d$gene <- rownames(d)
dd <- melt(d)
p <- ggplot(dd,aes(variable,gene,fill=value))+
geom_tile()+
scale_fill_viridis_c()+
scale_y_discrete(position = "right")+
theme_minimal()+
labs(x="",y="")
g <- ggplot(dplyr::filter(dd,gene!="g2"),
aes(gene,value,fill=gene))+
geom_boxplot()+
coord_flip()+
scale_fill_brewer(palette = "Set1")+
theme_minimal()+
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank())+
labs(x="",y="")
ca <- data.frame(variable=paste0("t",1:5),
A1=rep(LETTERS[1:2],times=c(3,2)),
A2=rep(letters[3:5],times=c(1,3,1)))
cad <- gather(ca,A1,A2,key = "anno",value = "type")
pc <- ggplot(cad,aes(variable,anno,fill=type))+
geom_tile()+
scale_y_discrete(position = "right")+
theme_minimal()+
theme(axis.text.x = element_blank(),
axis.ticks.x=element_blank())+
labs(x="",y="")
dp <- data.frame(gene=factor(rep(paste0("g",1:5),2)),
pathway=sample(paste0("pathway",1:5),10,replace = T))
pp <- ggplot(dp,aes(pathway,gene))+
geom_point(size=5,color="steelblue")+
theme_minimal()+
theme(axis.text.x=element_text(angle=90,hjust=0),
axis.text.y = element_blank(),
axis.ticks.y=element_blank())+
labs(x="",y="")
p %>% insert_left(phr,width = .3) %>%
insert_right(pp,width = .3) %>%
insert_right(g,width = .4) %>%
insert_top(pc,height =.1) %>%
insert_top(phc,height = .2)
看图确实让人感觉眼花缭乱的,但逻辑比较清晰,就是先用ggplot2绘制所有图,然后通过aplot将这些图放在主图的周围。
3.实战二:散点图+边际密度图
rm(list = ls())
library(ggsci)
pp <- ggplot(iris,aes(Sepal.Length,Sepal.Width,color=Species))+
geom_point()+
theme_bw()+
scale_color_lancet()
x <- ggplot(iris,aes(Sepal.Length,fill=Species))+
geom_density(alpha=0.5)+
scale_fill_lancet()+
expand_limits(x=0,y=0)+
scale_y_continuous(expand = c(0,0))+
theme_classic()+
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x=element_blank())+
guides(fill=FALSE)
y <- ggplot(iris,aes(Sepal.Width,fill=Species))+
geom_density(alpha=0.5)+
scale_fill_lancet()+
expand_limits(x=0,y=0)+
scale_y_continuous(expand = c(0,0))+
theme_classic()+
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y =element_blank())+
guides(fill=FALSE)+
coord_flip()
pp %>%
insert_right(y,width = .2) %>%
insert_top(x,height = .3)
还可以用ggExtra
包直接展示
library(ggExtra)
p <- ggplot(iris,aes(Sepal.Length,Sepal.Width,color=Species))+
geom_point()+
guides(color=FALSE)+
theme_bw()
ggMarginal(p,type = "density",groupFill = T)
参考链接: