您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# R语言怎么实现矩阵气泡图
## 引言
矩阵气泡图(Matrix Bubble Chart)是一种将气泡图与矩阵布局相结合的数据可视化形式,能够同时展示多个变量之间的关系。在R语言中,通过基础绘图函数或`ggplot2`等扩展包可以高效实现这种图表。本文将详细介绍3种实现方法,并提供完整代码示例和优化技巧。
## 一、基础概念与数据准备
### 1.1 矩阵气泡图的特点
- **三维数据展示**:通过x/y轴定位气泡位置,气泡大小表示第三维度数值
- **矩阵布局**:通常用于展示相关系数矩阵或相似度矩阵
- **颜色映射**:可添加第四维度(如正负值)的颜色区分
### 1.2 示例数据集
使用R内置的`mtcars`数据集生成相关系数矩阵:
```r
data <- cor(mtcars)
melted_data <- reshape2::melt(data)
colnames(melted_data) <- c("Var1", "Var2", "value")
head(melted_data)
plot(melted_data$Var1, melted_data$Var2,
cex = abs(melted_data$value)*5, # 气泡大小
pch = 21,
bg = ifelse(melted_data$value > 0, "red", "blue"),
xlab = "", ylab = "",
main = "Base R Matrix Bubble Chart")
axis(1, at=1:ncol(data), labels=colnames(data), las=2)
axis(2, at=1:nrow(data), labels=rownames(data), las=1)
legend("topright",
legend = c("Positive", "Negative"),
pch = 21, pt.bg = c("red", "blue"))
library(ggplot2)
ggplot(melted_data, aes(x=Var1, y=Var2)) +
geom_point(aes(size=abs(value), fill=value), shape=21) +
scale_size(range = c(1, 10)) +
scale_fill_gradient2(low="blue", mid="white", high="red") +
theme_minimal()
ggplot(melted_data, aes(Var1, Var2)) +
geom_point(aes(size = abs(value), fill = value), shape = 21) +
scale_size_area(max_size = 12, guide = "none") +
scale_fill_gradient2(low = "#377EB8", high = "#E41A1C",
mid = "white", midpoint = 0) +
geom_text(aes(label = round(value, 2)), size = 3) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major = element_blank()) +
coord_fixed() +
labs(x = NULL, y = NULL)
# 添加分组变量
melted_data$group <- sample(LETTERS[1:3], nrow(melted_data), replace = TRUE)
ggplot(melted_data, aes(Var1, Var2)) +
geom_point(aes(size = abs(value), fill = value), shape = 21) +
facet_wrap(~group) +
scale_size(range = c(1, 8)) +
theme(axis.text.x = element_text(angle = 90))
library(plotly)
plot_ly(melted_data,
x = ~Var1, y = ~Var2,
size = ~abs(value),
color = ~value,
type = 'scatter',
mode = 'markers',
hoverinfo = 'text',
text = ~paste(Var1, "vs", Var2, "<br>Value:", round(value, 3)))
plot_ly(melted_data, x = ~Var1, y = ~Var2) %>%
add_markers(
size = ~abs(value)*20,
color = ~value,
colors = c("#2c7bb6", "#ffffbf", "#d7191c"),
hoverinfo = "text",
text = ~paste0("Correlation: ", round(value, 2))
# 相对比例法
melted_data$size_norm <- (abs(melted_data$value) - min(abs(melted_data$value))) /
(max(abs(melted_data$value)) - min(abs(melted_data$value)))
# 使用ggrepel避免标签重叠
library(ggrepel)
ggplot(melted_data, aes(Var1, Var2)) +
geom_point(aes(size = abs(value)), alpha = 0.5) +
geom_text_repel(aes(label = round(value, 2)), size = 3)
geom_point(shape = ".")
toWebGL()
函数# 模拟基因表达数据
set.seed(123)
gene_expr <- matrix(rnorm(100), nrow=10)
rownames(gene_expr) <- paste0("Gene", 1:10)
colnames(gene_expr) <- paste0("Sample", 1:10)
# 绘制气泡图
heatmaply::heatmaply(gene_expr,
cellnote = round(gene_expr, 2),
plot_method = "ggplot")
library(igraph)
g <- sample_gnp(10, 0.3)
adj_matrix <- as_adjacency_matrix(g)
ggplot(reshape2::melt(as.matrix(adj_matrix)),
aes(Var1, Var2, size = value)) +
geom_point(alpha = 0.7) +
theme_light()
方法 | 优点 | 缺点 | 适用场景 |
---|---|---|---|
Base R | 无需依赖包 | 定制化代码复杂 | 快速简单可视化 |
ggplot2 | 高度可定制 | 学习曲线较陡 | 发表级图形制作 |
plotly | 交互功能强大 | 输出文件较大 | 网页交互式报告 |
# 综合示例:ggplot2高级矩阵气泡图
library(ggplot2)
library(reshape2)
# 数据处理
cor_matrix <- cor(mtcars)
melted_cor <- melt(cor_matrix)
melted_cor$abs_value <- abs(melted_cor$value)
# 绘图
ggplot(melted_cor, aes(Var1, Var2)) +
geom_point(aes(size = abs_value, fill = value), shape = 21, color = "black") +
scale_size(range = c(1, 15), guide = "none") +
scale_fill_gradient2(low = "blue", high = "red", mid = "white",
midpoint = 0, limits = c(-1, 1)) +
geom_text(aes(label = round(value, 2)), color = "black", size = 3) +
theme_minimal(base_size = 12) +
theme(axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid = element_blank()) +
coord_fixed() +
labs(title = "汽车数据集相关系数矩阵气泡图",
x = "", y = "", fill = "Correlation")
”`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。