Измените ориентацию диагонали графика корреляции с помощью пакета ggcorrplot - если тип выше или ниже


person Poza    schedule 14.05.2021    source источник
comment
Я посмотрел на ggorrplot; это кажется врожденным для некоторых строк кода, которые они выполняют внутри. Проще всего было бы просто переключиться на другой пакет (согласно ответу @elielink) или изменить базовый код. Но, возможно, я что-то упускаю.   -  person slamballais    schedule 14.05.2021


Ответы (2)


Вы можете построить corr.mat с geom_tile вручную:

library(data.table)
library(ggplot2)
cordt <- as.data.table(corr.mat, keep.rownames = 'col_name')
cordt <- melt(cordt, id.vars = 'col_name', variable.name = 'row_name')

# convert to factor so that rows and columns have the same order as the data
cordt[, row_name := factor(row_name, levels = rev(rownames(corr.mat)))]
cordt[, col_name := factor(col_name, levels = rownames(corr.mat))]

# set diagonal and the top-right half of the matrix to 0 so that those cells appears white
cordt[ncol(corr.mat) - as.integer(row_name) < as.integer(col_name), value := 0]
# remove the last column and the bottom row (where left cells are self correlations only)
cordt <- cordt[as.integer(row_name) < ncol(corr.mat) &
        as.integer(col_name) < ncol(corr.mat)]

ggplot(cordt, aes(x = col_name, y = row_name, fill = value)) +
    geom_tile() +
    scale_fill_gradient2(low = 'blue', high = 'red') +
    labs(x = NULL, y = NULL, fill = 'Corr') +
    theme_minimal()

введите здесь описание изображения

person mt1022    schedule 14.05.2021

Когда я хочу показать только половину матрицы корреляции, я использую это (из пакета GGally):

ggcorr(mtcars[, c("mpg", "disp", "hp", "drat", "wt", "carb")], method = c('everything', "p"))

Однако диагональ не совсем то, что вы хотите. Может есть возможность перевернуть

person elielink    schedule 14.05.2021