简体   繁体   English

我如何使用 colnames 作为chart.Correlation 的标签,如 corrplot

[英]How do i use colnames as labels for chart.Correlation like in corrplot

I love the correlation plot from chart.Correlation but for multiple columns the names aren't readable and the values from the single correlation plots aren't that important anymore.我喜欢 chart.Correlation 中的 plot 的相关性,但是对于多个列,名称不可读,并且单个相关图中的值不再那么重要。 So is there a way to define the labels in chart.Correlation to how they are in corrplot?那么有没有办法在图表中定义标签。与它们在 corrplot 中的相关性?

library(corrplot)
library(xts)
library(PerformanceAnalytics)

dat <- xts(matrix(rnorm(1000), ncol = 4), order.by = as.Date(1:250))
colnames(dat) <- c("Name1", "Name2", "Name3", "Name4")

cor <- cor(dat)
corrplot(cor, method = "number")
chart.Correlation(dat)

在此处输入图像描述 在此处输入图像描述

Update:更新:

I got the solution for some of it but i am stuck with the alignment of mtext()我得到了一些解决方案,但我坚持使用 mtext() 的 alignment

custom.chart.Correlation <- function (R, histogram = TRUE, method = c("pearson", "kendall", 
                                          "spearman"), ...) 
{
  x = checkData(R, method = "matrix")
  if (missing(method)) 
    method = method[1]
  panel.cor <- function(x, y, digits = 2, prefix = "", 
                        use = "pairwise.complete.obs", method = "pearson", 
                        cex.cor = 1, ...) {
    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(round(r,2), 0.123456789), digits = digits)[1]
    txt <- paste(prefix, txt, sep = "")
    if (missing(cex.cor)) 
      cex.cor <- 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.01, 0.05, 0.1, 1), symbols = c("***", 
    #                                                                           "**", "*", ".", " "))
    #Create a function to generate a continuous color palette
    rbPal <- colorRampPalette(c('red','blue'))
    text(0.5, 0.5, txt, cex = cex.cor, col = rbPal(100)[as.numeric(abs(r)*100)])
    # text(0.8, 0.8, Signif, cex = cex, col = 2)
  }
  f <- function(t) {
    dnorm(t, mean = mean(x), sd = sd.xts(x))
  }
  dotargs <- list(...)
  dotargs$method <- NULL
  rm(method)
  hist.panel = function(x, ... = NULL) {
    par(new = TRUE)
    hist(x, col = "light gray", probability = TRUE, 
         axes = FALSE, main = "", breaks = "FD")
    lines(density(x, na.rm = TRUE), col = "red", lwd = 1)
    rug(x)
  }
  if (histogram) 
    pairs(x, gap = 0, lower.panel = panel.smooth, upper.panel = panel.cor, 
          diag.panel = hist.panel, ...)
  else pairs(x, gap = 0, lower.panel = panel.smooth, upper.panel = panel.cor, ...)
  size = (par("usr")[2] - par("usr")[1])/1.08
  start <- par("usr")[1] + 0.04*size
  end <- par("usr")[2] - 0.04*size
  at <- seq(0.04, 1-0.04, length.out = ncol(R)+2)
  mtext(colnames(R), side = 3, at[2:ncol(R)+1] = at, cex = 0.5, las = 2)
}

custom.chart.Correlation(dat, pch = ".", cex.cor = 1, yaxt = 'n', xaxt = 'n', labels = "")

在此处输入图像描述

The real world example:现实世界的例子:

在此处输入图像描述

It is doable with a different package to create the plot.可以使用不同的 package 创建 plot。 GGally uses lots of ggplot features. GGally使用了很多ggplot特性。

library(GGally)
lowerFn <- function(data, mapping, method = "lm", ...) {
  p <- ggplot(data = data, mapping = mapping) +
    geom_point(colour = "black") +
    geom_smooth(method = method, color = "red", ...)
  p
}


ggpairs(dat, lower=list(continuous = wrap(lowerFn, method = "lm")),
        upper = list(continuous = "cor"),
        diag=list(discrete= "barDiag"), 
        axisLabels='none') +
  theme_bw() +
  theme(strip.text = element_text(angle=90, hjust=1),
        strip.background = element_rect(fill = NA),
        strip.text.y = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

Results in your prefered corrolationmatrix with the labels as in the corrplot.使用 corrplot 中的标签生成您喜欢的 corrolationmatrix。 For a nice example also see this question 一个很好的例子也看到这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ggplot2包创建此图表.R中的相关图? - How can I create this chart.Correlation graph in R using the ggplot2 package? 如何从chart.correlation函数中删除有效星 - How to remove significance stars from chart.correlation function 如何更改R中PerformanceAnalytics库中chart.Correlation的颜色? - How to change the colors of chart.Correlation from PerformanceAnalytics library in R? 如何在R corrplot中的相关矩阵的文本标签中包含多种颜色? - How can I include multiple colours in the text labels for a correlation matrix in R corrplot? 图表与连续和类别变量的相关性 - chart.Correlation with continious and categorical variables 如何增加 PerformanceAnalytics::chart.Correlation 和断线中变量名称的大小? - How to increase the size of the name of variables in PerformanceAnalytics::chart.Correlation and break line? 如何将 Corrplot 与手动创建的相关矩阵一起使用(列表类型) - How to use Corrplot with correlation matrix created by hand (of type list) 无法更改 R 图中的 pch(点符号)。相关性 - cannot change pch (point symbol) in R chart.Correlation 如何将相关图表保存到 object? - How do I save correlation chart to an object? 如何突出 R 中 corrplot 的显着相关性? - How can I highlight significant correlation in corrplot in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM