text(0.5, 0.8, paste("Hopkins statistic = ", round(hopkins_stat, 2))) Error in text.default(0.5, 0.8, paste("Hopkins statistic = ", round(hopkins_stat, : plot.new has not been called yet
时间: 2024-01-16 13:05:29 浏览: 149
这个错误通常在你试图在没有创建图形的情况下调用 "text" 函数时出现。在调用 "text" 函数之前,你需要先使用 "plot" 函数创建一个图形。例如:
```
plot(1:10)
text(5, 5, "Hello World!")
```
这将在一个新的设备上创建一个简单的图形,并在坐标(5, 5)处添加 "Hello World!" 的文本。确保在调用 "text" 函数之前使用 "plot" 函数创建一个图形。
相关问题
panel.cor <- function(x, y,digits=2, prefix="", use="pairwise.complete.obs", method=cormeth, cex.cor, ...) { usr <- par("usr"); on.exit(par(usr)) par(usr = c(0, 1, 0, 1)) r <- cor(x, y, use=use, method=method) # MG: remove abs here txt <- format(c(r, 0.123456789), digits=digits)[1] txt <- paste(prefix, txt, sep="") if(missing(cex.cor)) cex <- 0.8/strwidth(txt) test <- cor.test(as.numeric(x),as.numeric(y), method=method) # borrowed from printCoefmat Signif <- symnum(test$p.value, corr = FALSE, na = FALSE,cutpoints = c(0, 0.001, 0.005,0.01, 0.05,0.1,0.5, 1)) # MG: add abs here and also include a 30% buffer for small numbers corr_text <- round(cor(x, y), 2) symbol_size <- abs(Signif) * 10 color_scale <- colorRampPalette(c("#F09576","white","#175A9E")) # 颜色比例尺 symbol_color <- color_scale(5 + corr_text * 5) # 根据相关系数设置颜色 symbols(1, 1, circles = symbol_size, inches = FALSE, bg = symbol_color,fg = NA, add = TRUE) #symbols(mean(x), mean(y), circles = symbol_size,inches = FALSE,bg = symbol_color, fg = NA, add = TRUE) # 绘制符号 #text(0.5, 0.5,paste(corr_text), cex = 2 *( abs(corr_text) + .3) / 1.2) #text(0.5, 0.5, paste(corr_text), cex = )mean(x), max(y) * 0.9 text(0.5, 0.5, txt, cex = cex * (abs(r) + .3) / 1.3)怎么修改,使根据不同颜色圆圈表示相关性大小,且同时用不同大小圆圈表示显著性
可以尝试修改如下代码:
```
panel.cor <- function(x, y,digits=2, prefix="", use="pairwise.complete.obs", method=cormeth, cex.cor, ...) {
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- cor(x, y, use=use, method=method)
txt <- format(c(r, 0.123456789), digits=digits)[1]
txt <- paste(prefix, txt, sep="")
if(missing(cex.cor)) cex <- 0.8/strwidth(txt)
test <- cor.test(as.numeric(x),as.numeric(y), method=method)
Signif <- symnum(test$p.value, corr = FALSE, na = FALSE,cutpoints = c(0, 0.001, 0.005,0.01, 0.05,0.1,0.5, 1))
# 根据相关系数设置颜色和大小
corr_text <- round(cor(x, y), 2)
symbol_size <- abs(Signif) * 10 * (abs(corr_text) + 0.3) # 增加相关系数的权重
color_scale <- colorRampPalette(c("#F09576","white","#175A9E"))
symbol_color <- color_scale(5 + corr_text * 5)
# 绘制符号
symbols(1, 1, circles = symbol_size, inches = FALSE, bg = symbol_color, fg = NA, add = TRUE)
# 显示相关系数
text(0.5, 0.5, txt, cex = cex * (abs(r) + .3) / 1.3)
}
```
这个修改后的函数会根据相关系数的大小和显著性同时显示不同颜色和大小的圆圈,来表示相关性大小和显著性。其中,增加了相关系数的权重,以使圆圈大小更加准确地反映相关性大小。
阅读全文