边缘密度分布图 | ggExtra包/aplot拼图/ggpubr包 等的实现方法

19 篇文章 0 订阅

概述: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
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值