编辑相关图

时间:2018-02-05 18:24:41

标签: r pearson-correlation

我试图使用corrgram包创建一个相关图

library(corrgram)
data("iris")

col.corrgram <- function(ncol) {
  colorRampPalette(
    c("darkgoldenrod4","burlywood1","darkkhaki","darkgreen")
  )(ncol)
}

corrgram(
  iris,
  order = TRUE,
  lower.panel = panel.shade,
  upper.panel = panel.pts,
  col.regions = col.corrgram
)

变量名称出现在对角线面板中。我需要相关图左侧和底部的名称,并在对角线面板中创建每个变量的直方图。

有什么想法吗? 感谢

1 个答案:

答案 0 :(得分:0)

# From 'pairs' help page for plotting histogram
panel.hist <- function(x, ...)
{
  usr <- par("usr"); on.exit(par(usr))
  par(usr = c(usr[1:2], 0, 1.5) )
  h <- hist(x, plot = FALSE)
  breaks <- h$breaks; nB <- length(breaks)
  y <- h$counts; y <- y/max(y)
  rect(breaks[-nB], 0, breaks[-1], y, col = "cyan", ...)
}

# Load library
library(corrgram)

# Plot correlogram
tst <- corrgram(mtcars,
         order=TRUE, 
         diag.panel = panel.hist, 
         upper.panel = panel.pts,
         lower.panel = panel.shade,
         text.panel = NULL,
         outer.labels = list(bottom = list(labels = colnames(mtcars)), 
                             left = list(labels = colnames(mtcars))))