首页 > 解决方案 > 如何增加 PerformanceAnalytics::chart.Correlation 和折线中变量名称的大小?

问题描述

我想增加对角线中变量名称的字体大小(如果可能的话,还要换行)。我怎样才能做到这一点?我的尝试:

df <- structure(list(Coop. = c(3.75, 5, 4.75, 4, 5, 4.5, 4.75, 5, 4, 
5), `Papel Imp.` = c(3.6, 5, 4.6, 4.4, 5, 4.6, 4, 4.8, 4.6, 5
), `Esf./Melh.` = c(4.25, 5, 5, 3.625, 4.875, 4.125, 3.5, 4.5, 
4.75, 5), `Pun. Erros` = c(2.16666666666667, 3, 3.66666666666667, 
4.33333333333333, 2.66666666666667, 3.66666666666667, 3.33333333333333, 
3.16666666666667, 4, 2.16666666666667), `Recon. Desig.` = c(2.42857142857143, 
1.14285714285714, 1, 3.71428571428571, 1.42857142857143, 2.85714285714286, 
2.57142857142857, 1.42857142857143, 4, 1.14285714285714), `Riv. Membr.` = c(2.33333333333333, 
2.66666666666667, 5, 3.33333333333333, 3, 2.33333333333333, 4, 
3.33333333333333, 2.66666666666667, 2.66666666666667), `Média PREPARAÇÃO` = c(5.75555555555556, 
5.83333333333333, 6.65555555555556, 4.62222222222222, 6.31111111111111, 
5.7, 5.25555555555556, 6.65555555555556, 5.84444444444444, 6.83333333333333
), `Média EXECUÇÃO` = c(5.15, 6.16666666666667, 5.88333333333333, 
4.8, 6.93333333333333, 6.43333333333333, 5.58333333333333, 6.11666666666667, 
6.08333333333333, 6.43333333333333), `Média AVALIAÇÃO` = c(5.45833333333333, 
6.54166666666667, 6.625, 5.91666666666667, 5.45833333333333, 
6.25, 5.66666666666667, 6.625, 6.25, 6.75), `Média AJUSTES` = c(4.9, 
6.225, 6.95, 4.325, 6.7625, 6.4, 5.85, 6.125, 4.875, 6.5), `Média GME` = c(5.7, 
6.65, 6.75, 5.15, 6.875, 6.55, 5.925, 6.7, 5.225, 6.775), `Treinamento Instrução (TI)` = c(5.66666666666667, 
5, 7, 2, 7, 6.33333333333333, 4.66666666666667, 7, 6.66666666666667, 
7), `Tratamento Pessoal (TP)` = c(5.8, 7, 6.8, 2, 6.4, 5, 5.2, 
6.6, 5, 6.8), `Desempenho da Equipe` = c(3, 5, 5, 3.66666666666667, 
7, 5.66666666666667, 6, 6, 4.33333333333333, 6.33333333333333
), `Desempenho Individual` = c(5.33333333333333, 6.66666666666667, 
5.33333333333333, 5.33333333333333, 6.33333333333333, 5.66666666666667, 
5.33333333333333, 6.33333333333333, 5.66666666666667, 7)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

我的代码:

PerformanceAnalytics::chart.Correlation(df, method = "pearson", hist = F)

在此处输入图像描述

我知道这张图是基于pairs(),但这个问题的解决方案对我不起作用。

标签: rplot

解决方案


自定义函数

好吧,它不起作用,因为该功能被窃听。中的参数...不通过pairs

所以只需这样重写你自己的函数:


mychart.Correlation <- function (R, method = c("pearson", "kendall", "spearman"), ...){
 
 x = PerformanceAnalytics::checkData(R, method = "matrix")
 if (missing(method)) method = method[1]
 cormeth <- method
 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.01, 0.05, 0.1, 1),
                   symbols = c("***", "**", "*", ".", " "))
  text(0.5, 0.5, txt, cex = cex * (abs(r) + 0.3)/1.3)
  text(0.8, 0.8, Signif, cex = cex, col = 2)
 }
 
 pairs(x, gap = 0, lower.panel = panel.smooth, upper.panel = panel.cor, ...)
 
}

names(df) <- gsub(" ", "\n", names(df))
mychart.Correlation(df, method = "pearson", cex.labels = 1.5)

在此处输入图像描述


开发者解决方案

您可以看到,在包的GitHub 版本中,这个问题已经解决(...正确存在pairs),而在CRAN 版本中没有...

所以解决你的问题的另一种方法是这样安装GitHub版本的包:

devtools::install_github("braverock/PerformanceAnalytics")

names(df) <- gsub(" ", "\n", names(df))
PerformanceAnalytics::chart.Correlation(df, method = "pearson", hist = F, cex.labels=1.5)

推荐阅读