如何在R中绘制稀疏矩阵?

时间:2019-06-17 12:50:03

标签: r matrix plot

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

enter image description here

但是它没有指定如何获取图像。我已经尝试过使用plot.matrix包和SparseM包,但仍然找不到答案。

2 个答案:

答案 0 :(得分:0)

您可以在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"))

enter image description here

答案 1 :(得分:0)

您还可以按照以下方式进行操作:

  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')