概述:aplot 拼图效果好
根据网友探索[1],总结如下:
- ggExtra 包的拼图间隙有点大,图例在主图和边缘图之间,除非去掉图例,否则没法看。
- aplot包的默认拼图间隙很小,比较美观,图例在外面。
- gridExtra包默认拼图之间的间隙比较大。
- 另外仔细比较aplot包和gridExtra包的拼图结果,可以发现aplot包是把子图的panel区域对齐,而gridExtra包是把子图的plot区域对齐。
- 结论:aplot包的对齐效果显然是我们需要的。
1. ggExtra 画边缘密度图
# 1. get data
set.seed(2024)
dat=diamonds[sample(1:nrow(diamonds), 1000),]
library(ggplot2)
# 2. base plot
# with legend
p1=ggplot(dat, aes(carat, price, color=clarity))+geom_point()+theme_classic()+theme(panel.border = element_rect(size = 1, colour = "black", fill="#00112200")); p1
# no legend
p2=ggplot(dat, aes(carat, price, color=clarity))+geom_point(show.legend = F)+theme_classic()+theme(panel.border = element_rect(size = 1, colour = "black", fill="#00112200")); p2# 3.margin density
library(ggExtra)
ggExtra::ggMarginal(p1+ggtitle("p1-A"), type = "density")
ggMarginal(p1+ggtitle("p1-B"), colour = "red")# no legend
ggMarginal(p2+ggtitle("p2-A"), groupColour = TRUE)
ggMarginal(p2+ggtitle("p2-B"),type = "density",groupColour = TRUE,groupFill = TRUE)
ggMarginal(p2+ggtitle("p2-C"), xparams=list( fill = c("deeppink")),yparams=list( fill = c("navy")),color=NA)
2. ggplot2绘图+aplot拼图
library(RColorBrewer)
len=length( unique(dat$clarity) ); len #8# upper density
top_panel <- ggplot()+geom_density(data = dat,mapping=aes(carat, fill=clarity, color=clarity),alpha=0.5)+scale_fill_manual(values = brewer.pal(len,'Set2'))+ #设置填充颜色scale_color_manual(values = brewer.pal(len,'Set2'))+ # 设置线的颜色theme_void()+ # 设置主题theme(legend.position="none") # 去除图例
top_panel# 右侧密度分布图
right_panel <- ggplot()+geom_density(data=dat,aes(price, fill=clarity,color=clarity),alpha=0.5)+scale_fill_manual(values = brewer.pal(len,'Set2'))+ #设置填充颜色scale_color_manual(values = brewer.pal(len,'Set2'))+ # 设置线的颜色theme_void()+ # 设置主题coord_flip()+ # 翻转坐标轴theme(legend.position="none")# 去除图例
right_panel# aplot 包拼图
library(aplot)# no legend
p2 %>% insert_right(right_panel, width=.4) %>% insert_top(top_panel, height=0.2) # with legend
p1 %>% insert_right(right_panel, width=.4) %>% insert_top(top_panel, height=0.2) #ggsave('plot2.pdf',width = 6,height = 5)
3. ggpubr 包绘制边缘密度分布图
# fig1
library(ggpubr)
gp1<-ggscatterhist(iris, x = "Sepal.Length", y = "Sepal.Width",color = "Species", size = 3, alpha = 0.6)
gp1
# 不支持theme()
# gp1+theme(panel.background = element_rect(fill="#00112200", color="black"))# fig2
set.seed(2024)
dat=diamonds[sample(1:nrow(diamonds), 1000),]ggscatterhist(dat, x = "carat", y = "price",color = "clarity", size = 2, alpha = 0.6)# fig3
ggscatterhist(dat, x = "carat", y = "price",color = "clarity", size = 3, alpha = 0.6,#palette = c("#00AFBB", "#E7B800", "#FC4E07"),margin.params = list(fill = "clarity", color = "black", size = 0.2)
)# Fig4:指定主图黑边框(略)
ggscatterhist(dat, x = "carat", y = "price",color = "clarity", size = 3, alpha = 0.6,#palette = c("#00AFBB", "#E7B800", "#FC4E07"),margin.params = list(fill = "clarity", color = "black", size = 0.2),ggtheme = theme_classic() + #指定周围边框theme(panel.background = element_rect(fill="#00112200", color="black"))
)
Ref
- [1] 分组p值箱线图 https://blog.csdn.net/weixin_46587777/article/details/138032368
- [2] https://cran.r-project.org/web/packages/ggExtra/ggExtra.pdf
- [3] https://cran.r-project.org/web/packages/ggExtra/vignettes/ggExtra.html