Editor's Note
The following article is from 被炸熟的虾 Author 不神奇的海螺
被湿实验室扫地出门的生信学徒,记录自己的学习笔记与“虾想”时刻
前期已经整理常规散点图的绘制方法,现在我们尝试在侧面添加密度曲线/数据分布直方图。
首先绘制添加拟合曲线的散点图,依然使用iris示例数据。为了更好显示不同分组的数据分布差异,我们对不同分组分别添加拟合曲线及回归方程:
stat_poly_line:在 ggplot2 图形中添加多项式回归线。这个函数直接计算多项式回归模型,并将拟合线添加到图形上。它允许指定多项式的阶数,即回归方程中最高次项的次数。
stat_poly_eq:添加多项式回归方程和相关统计量(如R2、p值等)的标签。
library(ggplot2)
library(ggpmisc)
theme <- theme_bw()+
theme(plot.title = element_text(hjust = 0.5,size = 22),
axis.text.x = element_text(hjust = 0.5,size = 20),
axis.text.y = element_text(hjust = 0.5,size = 20),
axis.title.y = element_text(size = 20),
axis.title.x = element_text(size = 20),
legend.text = element_text(size = 22),
legend.title = element_blank(),
legend.position = c(0.15,0.8),
legend.background = element_blank())
p1 <- ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width, group = Species)) +
geom_point(aes(color = Species), size = 4) +
scale_x_continuous(limits = c(1,7), breaks = seq(1,7,1),labels = seq(1,7,1)) +
scale_y_continuous(limits = c(0,2.5), breaks = seq(0,2.5,0.5),labels = seq(0,2.5,0.5)) +
stat_poly_line(aes(color = Species),formula = y ~ x,show.legend = FALSE) + # 添加线性回归线
stat_poly_eq(aes(color = Species,
label = paste(..eq.label.., ..rr.label.., stat(p.value.label), sep = "*\"; \"*")),
formula = y ~ x, label.x.npc = 'right', label.y.npc = 'bottom',
size = 6, rr.digits = 3) +
scale_color_manual(values = c("#4d97cd","#459943","#c74546")) +
theme
p1 + guides(colour = guide_legend(override.aes = list(size = 5)))##调整图例大小
pdensity <- ggMarginal(p1, type = "density", groupColour = TRUE, groupFill = TRUE, alpha = 0.75, bins = 30)
pdensigram <- ggMarginal(p1, type = "densigram", groupColour = TRUE, groupFill = TRUE, alpha = 0.75, bins = 30)
phistogram <- ggMarginal(p1, type = "histogram", groupColour = TRUE, groupFill = TRUE, alpha = 0.75, bins = 30)
pboxplot <- ggMarginal(p1, type = "boxplot", groupColour = TRUE, groupFill = TRUE, alpha = 0.75)
pviolin <- ggMarginal(p1, type = "violin", groupColour = TRUE, groupFill = TRUE, alpha = 0.75)
theme2 <- theme_classic() +
theme(plot.title = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.text = element_blank(),
legend.title = element_blank(),
legend.position = "none",
legend.background = element_blank())
###top
p2 <- ggplot(data = iris,aes(x = Petal.Length,fill = Species)) +
geom_histogram(bins = 30,color = "black",alpha = 0.75) +
scale_x_continuous(limits = c(1,7)) +
scale_y_continuous(expand = c(0,0.2)) +
scale_fill_manual(values = c("#4d97cd","#459943","#c74546")) +
theme2
###right
p3 <- ggplot(data = iris,aes(x = Petal.Width,fill = Species)) +
geom_histogram(bins = 30,color = "black",alpha = 0.75) +
scale_x_continuous(limits = c(0,2.5)) +
scale_y_continuous(expand = c(0,0.2)) +
coord_flip() +
scale_fill_manual(values = c("#4d97cd","#459943","#c74546")) +
theme2
library(aplot)
p4 <- p1 %>% insert_right(p3, width=.2) %>% insert_top(p2, height=0.2)