【问题标题】:How to plot a sparse matrix in R?如何在 R 中绘制稀疏矩阵?
【发布时间】:2019-10-31 01:40:27
【问题描述】:

我有一个大型稀疏矩阵(100 行 8000 列),我想以图形方式表示它。我在互联网上发现了这种矩阵的这种表示:

但它没有说明图像是如何获得的。我尝试过使用 plot.matrix 包和 SparseM 包,但我仍然没有找到答案。

【问题讨论】:

  • 当您说“稀疏矩阵”时,您指的是哪种对象? Matrix 包里的东西?

标签: r matrix plot


【解决方案1】:

你可以在R中使用image()函数:

# Create a matrix with random 0s and 1s
pseudo.data <- rbinom(100 * 8000, 1, 0.5)
pseudo.data <- matrix(pseudo.data, nrow = 100)

# plot the matrix
image(t(pseudo.data), col = c("white", "black"))

【讨论】:

  • 我认为这不适用于巨大的矩阵。
  • 适用于 100x8000 矩阵。我更新了我的帖子。
【解决方案2】:

您也可以按照以下方式做一些事情:

  library(tidyverse)

generatedMatrix <- matrix(rnorm(900), ncol = 30)>.5

generatedMatrix %>% as.vector %>% 
  tibble(value = ., row = rep(1:nrow(generatedMatrix), times = ncol(generatedMatrix)),
                                         col = rep(1: ncol(generatedMatrix), each = nrow(generatedMatrix))) %>%
  ggplot(aes(x = row, y = col, colour = value)) +
  geom_point(size = 2) +
  scale_color_manual(values = c('black','white'))+
  theme_minimal()

  library(tidyverse)

generatedMatrix <- matrix(rbinom(900,size = 1,prob = .5), ncol = 30)

generatedMatrix %>% as.vector %>% 
  tibble(value = ., row = rep(1:nrow(generatedMatrix), times = ncol(generatedMatrix)),
                                         col = rep(1: ncol(generatedMatrix), each = nrow(generatedMatrix))) %>%
  ggplot(aes(x = row, y = col, fill = value)) +
  geom_tile(size = 2) +
  scale_fill_gradient(low = 'black',high = 'white')+
  theme_minimal() +
  theme(legend.position = 'none')

【讨论】:

  • 我认为这不适用于巨大的矩阵。
  • 我的意思是……有点……:D
  • 这样的情节有什么用? xD
猜你喜欢
  • 1970-01-01
  • 2017-07-01
  • 2020-10-29
  • 1970-01-01
  • 2018-09-20
  • 1970-01-01
  • 2014-05-22
  • 2018-12-14
  • 1970-01-01
相关资源
最近更新 更多