最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - corrplot negative sign rather than hyphen - Stack Overflow

programmeradmin2浏览0评论

To align with journal formatting purposes I have been requested to change hyphen (-, the default as displayed for negative correlations in R and corrplot) with a negative sign (, U+2212).

I've tried doing this as follows:

library(corrplot)

data(mtcars)
corr_matrix <- cor(mtcars)[1:2, 1:2]

format_coef <- function(coef) {
  coef <- sprintf("%.2f", coef) # Round to two dp
  coef <- gsub("-", "−", coef) # Replace hyphen with true negative sign
  return(coef)
}

# Format coefficients for display
formatted_labels <- apply(corr_matrix, c(1, 2), format_coef)

# Create the correlation plot
corrplot(corr_matrix, method = "circle", type = "full", 
         tl.col = "black", tl.cex = 0.8, 
         addCoef.col = NULL, number.cex = 3)

text(rep(1:ncol(corr_matrix), each = nrow(corr_matrix)), 
     rep(nrow(corr_matrix):1, times = ncol(corr_matrix)), 
     labels = formatted_labels, cex = 3)

though only the positive correlations are printed with this approach, as shown here:

What am I missing? Is there a better way to do this?

发布评论

评论列表(0)

  1. 暂无评论