You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用R绘制双矩阵对比散点图(Type1为X轴、Type2为Y轴)

用R绘制Type1与Type2的对比散点图

第一步:创建矩阵数据

先把你提供的矩阵在R中定义出来:

# 构建Type1矩阵
Type1 <- matrix(c(1,2,3,4,
                  2,1,7,8,
                  3,7,1,9,
                  4,8,9,1),
                nrow = 4, byrow = TRUE,
                dimnames = list(c("A","B","C","D"), c("A","B","C","D")))

# 构建Type2矩阵
Type2 <- matrix(c(1,12,13,14,
                  12,1,17,18,
                  13,17,1,19,
                  14,18,19,1),
                nrow = 4, byrow = TRUE,
                dimnames = list(c("A","B","C","D"), c("A","B","C","D")))

第二步:整理成对的X-Y数据

散点图需要一一对应的X、Y向量,直接把矩阵转为向量即可:

x_data <- as.vector(Type1)
y_data <- as.vector(Type2)

第三步:绘制散点图

提供两种实现方式,按需选择:

方式1:R基础绘图系统(快速简洁)

plot(x_data, y_data, 
     main = "Type1 vs Type2 散点图",
     xlab = "Type1", ylab = "Type2",
     pch = 16, col = "steelblue",
     cex = 1.2)
# 添加参考对角线(方便对比数值差异)
abline(a = 0, b = 1, lty = 2, col = "red")

方式2:ggplot2包(精致美观)

如果未安装ggplot2,先执行安装命令:

install.packages("ggplot2")

再绘制可视化图表:

library(ggplot2)

# 转换为数据框格式(ggplot要求的输入格式)
plot_df <- data.frame(Type1 = x_data, Type2 = y_data)

ggplot(plot_df, aes(x = Type1, y = Type2)) +
  geom_point(color = "steelblue", size = 3) +
  geom_abline(slope = 1, intercept = 0, linetype = "dashed", color = "red") +
  labs(title = "Type1 vs Type2 散点图",
       x = "Type1", y = "Type2") +
  theme_minimal()

内容的提问来源于stack exchange,提问作者user20505088

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 22:10:02