首页 > 解决方案 > 如何绘制双精度型的 Spearman 相关性

问题描述

我正在尝试绘制两个高度相关的变量之间的 Spearman 相关性,但我得到了一个“双重”类型的错误。

错误 - 尝试在两个变量之间使用 spearman 相关性时,不能强制“pairlist”对象键入“double”

我已经对数据进行了子集化,将两列转换为数字数据,并尝试将数据框转换为矩阵,但我仍然遇到同样的错误。

   library(ggpubr)
var_sub_set <- as.data.frame(bc_data[ ,c("compactness_mean", "points_mean" )])


Var <- as.data.frame(sapply(var_sub_set, as.numeric))

ggscatter(Var, x = "compactness_mean", y = "points_mean", 
          add = "reg.line", conf.int = TRUE, 
          cor.coef = TRUE, cor.method = "spearman",
          xlab = "Compactness Mean", ylab = "Points Mean")

数据应绘制在散点图上并显示 p_value 和任何线性线。

dput sample output

structure(list(compactness_mean = c(0.06981, 0.1147, 0.07804, 
0.1136, 0.06934, 0.07722, 0.1021, 0.1768, 0.08578, 0.09097, 0.1644, 
0.06679, 0.1089, 0.03789, 0.09871, 0.133, 0.09462, 0.07664, 0.1838, 
0.01781, 0.04304, 0.02749, 0.01867, 0.01471, 0.08543, 0.0444, 
0.02068, 0.1562, 0.02069, 0.063, 0.02583, 0.0226, 0.05598, 0.01692, 
0.01737, 0.034, 0.0287, 0.02037, 0.03263, 0.06495, 0.1255)), .Names = c("compactness_mean", 
"points_mean"), row.names = c(NA, -569L), class = "data.frame")

标签: r

解决方案


代码是正确的。重新安装 ggplot2 和 ggpubr 解决了这个问题。


推荐阅读