2017-10-12 65 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)]很棒,如果你想要一个灰度。