我如何使用 colnames 作为chart.Correlation 的标签,如 corrplot
Posted
技术标签:
【中文标题】我如何使用 colnames 作为chart.Correlation 的标签,如 corrplot【英文标题】:How do i use colnames as labels for chart.Correlation like in corrplot 【发布时间】:2021-07-31 18:36:33 【问题描述】:我喜欢 chart.Correlation 中的相关图,但对于多个列,名称不可读,并且单个相关图中的值不再那么重要。那么有没有办法在图表中定义标签。与它们在 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)
更新:
我得到了一些解决方案,但我坚持 mtext() 的对齐方式
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 = "")
现实世界的例子:
【问题讨论】:
【参考方案1】:可以使用不同的包来创建情节。 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())
生成您喜欢的 corrolationmatrix,其标签与 corrplot 中的标签相同。 For a nice example also see this question
【讨论】:
不幸的是,我不是很喜欢 ggplot,也找不到任何关于如何旋转这些标签的信息.. @Chris 你的意思是这样吗? ***.com/questions/46864196/ggpairs-rotate-axis-label @ Annet no 我想旋转轴名称,在该示例中称为 Type、Price、MPG.city、MPG.highway、EngineSize 和 Origin @Chris 只是将axis.text.x 更改为strip.text。只是该逻辑仅适用于图表的不同部分。看我的回答....以上是关于我如何使用 colnames 作为chart.Correlation 的标签,如 corrplot的主要内容,如果未能解决你的问题,请参考以下文章
R 中 data.table 的 colnames() 行为
R语言使用colnames函数改变dataframe列名称实战
sql2000行转列 转过来的测试完也不知那个网站去哪了 没法写出处了
Apache Spark :org.apache.spark.sql.Dataset.drop(String... colNames) 方法用于 Java