带有侧边密度图的相关性点图(自力更生版)

2024-04-06  本文已影响0人  小洁忘了怎么分身

0.需求

就是在点图的侧边加上密度图



原来是ggstatplot可以直接画的,但这个包的最新版本画图直接报错,旧版本可以装但是各种套娃,依赖包各种过时各种限制版本,搞起来真的麻烦。所以换个思路,自己攒代码吧

1.示例数据

library(ggpubr)
library(gridExtra)

set.seed(14)
gene1 = rnorm(100,sd = 18)
gene2 = gene1 + runif(100,min = 10,max = 50)
dat <- data.frame(gene1 = gene1, 
                  gene2 = gene2)
head(dat)

##         gene1    gene2
## 1 -11.9132969 36.27108
## 2  30.9411748 75.22487
## 3  38.1900058 77.98807
## 4  26.9487663 63.47728
## 5  -0.6505304 28.47225
## 6  22.1750132 62.65963

2.画图

p1 <- ggscatter( dat, x = "gene1", y = "gene2",
           add = "reg.line", conf.int = TRUE,
           add.params = list(color = "blue", fill = "lightgray"))+
  stat_cor()+
  theme_bw()

p2 <- ggplot(dat, aes(gene1)) +
  geom_density(fill = "#ff820e") +
  theme_void()

p3 <- ggplot(dat, aes(gene2)) +
  geom_density(fill = "#0000fe") +
  coord_flip() +
  theme_void()

3.拼图

library(patchwork)
empty_plot <- plot_spacer()
f = c("AAAAD
       BBBBC
       BBBBC
       BBBBC
       BBBBC")
p2+p1+p3+ empty_plot+plot_layout(design = f)
上一篇下一篇

猜你喜欢

热点阅读