我有这个稀疏的平方矩阵,它包含平方相关。
tmp <- readRDS(url("https://www.dropbox.com/s/65u96jf7y32j2mj/spMat.rds?raw=1"))
Matrix::image(tmp)
此矩阵是超稀疏的,并且仅在对角线周围具有非零值。我想做另一个表示,看起来像这样(忘记轴):
所以,基本上,我只想查看上三角形,旋转45°并且高度有限。
任何人都知道如何做到这一点?
答案 0 :(得分:1)
这不是同样的情节,而是非常相似:
## 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 <- tmpT@i < tmpT@j
coords <- cbind(tmpT@i[upper], tmpT@j[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, tmpT@x[upper]), ylim = c(0, 50), asp=2)
答案 1 :(得分:1)
根据@ AEF的回答,我做了这个:
library(tidyverse)
## Transform sparse representation into (i,j,x) triplets
tmpT <- as(tmp, "dgTMatrix")
upper <- (tmpT@i <= tmpT@j)
df <- data.frame(
i = tmpT@i[upper],
j = tmpT@j[upper],
r2 = tmpT@x[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')
PS:RColorBrewer::brewer.pal(9, "Greys")[-(1:2)]
非常棒,如果你想要一个灰度级。