2017-10-12 1 views
2

J'ai cette matrice carrée qui contient une corrélation au carré.Modifier la représentation graphique d'une matrice de corrélation clairsemée

tmp <- readRDS(url("https://www.dropbox.com/s/65u96jf7y32j2mj/spMat.rds?raw=1")) 
Matrix::image(tmp) 

enter image description here

Cette matrice est super rare et ont des valeurs non nulles situées uniquement autour de la diagonale. Je voudrais faire une autre représentation, qui ressemblerait à ceci (oublier les axes):

enter image description here

Donc, au fond, je veux voir que le triangle supérieur, tourné de 45 ° et avec une hauteur limitée .

Quelqu'un sait-il comment faire?

Répondre

1

Il n'est pas exatly la même parcelle, mais assez similaire:

## Transform sparse representation into (i,j,x) triplets 
tmpT <- as(tmp, "dgTMatrix") 

## get the "coordinates" of the non-0 elements in the upper triangle and rotate them by 45° 
upper <- [email protected] < [email protected] 
coords <- cbind([email protected][upper], [email protected][upper]) 
coords <- t(matrix(c(sqrt(2), -sqrt(2), sqrt(2), sqrt(2))/2, ncol = 2) %*% t(coords)) 

## plot the rotated coordinates and take the transparency from the value 
plot(coords, cex=.4, pch=18, col=rgb(0, 0, 0, [email protected][upper]), ylim = c(0, 50), asp=2) 

enter image description here

+0

Cela semble bon. Désolé, je ne serai pas en mesure de vérifier cela avant lundi. –

+0

Merci pour votre réponse. Cela m'inspire pour ma solution. –

1

Basé sur @ réponse de FEA, je fait ceci:

library(tidyverse) 
## Transform sparse representation into (i,j,x) triplets 
tmpT <- as(tmp, "dgTMatrix") 
upper <- ([email protected] <= [email protected]) 
df <- data.frame(
    i = [email protected][upper], 
    j = [email protected][upper], 
    r2 = [email protected][upper] 
) %>% 
    mutate(y = (j - i)/2) 

ggplot(df) + 
    geom_point(aes(i + y, y, color = r2, alpha = r2), size = rel(0.5)) + 
    coord_fixed() + 
    scale_color_gradientn(colours = rev(colorRamps::matlab.like2(100))) + 
    theme(axis.text.y = element_blank(), axis.ticks.y = element_blank()) + 
    labs(x = "Position", y = NULL) + 
    scale_alpha(guide = 'none') 

enter image description here

PS : RColorBrewer::brewer.pal(9, "Greys")[-(1:2)] est génial si vous voulez un échelle de gris.