0.需求
就是在點(diǎn)圖的側(cè)邊加上密度圖
原來是ggstatplot可以直接畫的,但這個(gè)包的最新版本畫圖直接報(bào)錯(cuò),舊版本可以裝但是各種套娃,依賴包各種過時(shí)各種限制版本噪生,搞起來真的麻煩。所以換個(gè)思路东囚,自己攢代碼吧
1.示例數(shù)據(jù)
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)