该内容已被发布者删除 该内容被自由微信恢复。
文章于 2024年4月4日 被检测为删除。
被用户删除
使用ggExtra包的ggMarginal函数添加边缘数据分布,需要关注以下几个关键参数: 依然使用ggplot2绘制分布图,使用aplot拼接。绘制时去除坐标轴等无关要素,确认坐标轴框选范围一致。这里仅测试分布直方图geom_histogram: 使用aplot拼图: 请点个“赞”和“在看”鼓励一下吧 被炸熟的虾 自己的摸索,发现问题麻烦告诉作者,光速回来改正
其他
R语言画图 | 散点图添加密度分布
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)))##调整图例大小
A、ggExtra
type:边缘图类型,包括density、histogram、boxplot、violin与densigram等 margins:选择绘图边,可选项:c("both", "x", "y") size = 5:边缘图与主图的相对大小,整数,默认为 5 ,即主图比边缘图宽 5 倍/高 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)
B、手动绘制拼图
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)