2017-10-12 88 views
2

我有這個包含平方相關的稀疏平方矩陣。更改稀疏相關矩陣的圖形表示

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

enter image description here

這個矩陣是超稀植,並有位於大約只有對角非零值。我想使另一個表示,其是這樣的(忘記軸):

enter image description here

所以,基本上,我想查看只有上部三角形,45°,並用有限的高度旋轉。

任何人都知道如何做到這一點?

回答

1

這是不exatly同積但相當類似:

## 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

看起來不錯。對不起,我無法在星期一之前查看。 –

+0

感謝您的回答。它激發了我的解決方案。 –

1

基於@ AEF的答案,我做了這個:

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)]很棒,如果你想要一個灰度。