【绘图】相关性散点图

Last updated on April 25, 2026 pm

配置环境

示例

  • 加载依赖
1
2
3
4
5
6
require(ggplot2)
require(ggpubr)
require(tidyverse)
require(RColorBrewer)
options(repr.plot.width=6, repr.plot.height=12)
RColorBrewer::display.brewer.all(type = "all")
  • 读入数据
1
2
data <- iris
data %>% summary
  • 选择数据
1
2
3
4
5
df <- data
yName <- 'Petal.Length'
ylim <- c(1, 7)
xName <- 'Petal.Width'
subName <- 'Species'
  • 绘图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
options(repr.plot.width=6, repr.plot.height=5)
ggscatter(
data = df[c(xName, yName, subName)] %>% na.omit(),
x = xName,
y = yName,
conf.int = T,
cor.coef = T,
cor.method = 'pearson',
color = subName,
size = 2,
alpha = 0.5
) +
scale_color_manual(values = RColorBrewer::brewer.pal(3, "Dark2")) +
stat_smooth(method = 'lm', se = T, color = 'grey') +
lims(y = ylim) + theme_bw() +
theme(panel.grid = element_blank(), legend.position = "right")


【绘图】相关性散点图
https://hexo.limour.top/scatter-plot-of-correlation
Author
Limour
Posted on
April 25, 2026
Updated on
April 25, 2026
Licensed under