大数据集的数据可视化

数据挖掘 大数据 可视化
2022-02-27 20:21:24

在处理一些海量数据集时,我对降维和绘图的方法很感兴趣。我偶然发现了这种新技术:UMAP(https://arxiv.org/pdf/1802.03426.pdf),它允许减少数据集的维度以将其绘制为二维。它看起来又快又高效。

在主要论文中,作者甚至提供了一些精美的情节。例如:

在此处输入图像描述

我一直无法使用 ggplot2 在 R 中重现这种图形(由于点太多、重叠点导致计算机崩溃......)。一个人将如何建立一个类似的情节?

2个回答

正如您所发现的,Ggplot2 非常适合简单的可视化,但不能很好地处理大型数据集。内存不足通常也是大集合的一个问题,计算 3000 万个整数的素数可分性绝对算得上是巨大的。

如果您可以访问更强大的系统或云平台,它们应该会更好地工作。一些 AWS 和 Azure 等解决方案并不昂贵,因此您可以尝试其中一种。

以下似乎运行得非常顺利,如果需要,可以轻松地重新参数化。

关键是建立一个新主题以应用于 ggplot2 图形并使用大小选项 (size=1, shape=".") :

library(ggplot2)

theme_black = function(base_size = 12, base_family = "") {

  theme_grey(base_size = base_size, base_family = base_family) %+replace%

    theme(
      # Specify axis options
      axis.line = element_blank(),  
      axis.text.x = element_blank(),  
      axis.text.y = element_blank(),  
      axis.ticks = element_blank(),  
      axis.title.x = element_blank(),  
      axis.title.y = element_blank(),  
      axis.ticks.length = unit(0.3, "lines"),   
      # Specify legend options
      legend.background = element_rect(color = NA, fill = "black"),  
      legend.key = element_blank(),  
      legend.key.size = unit(1.2, "lines"),  
      legend.key.height = NULL,  
      legend.key.width = NULL,      
      legend.text = element_text(size = base_size*0.8, color = "white"),  
      legend.title = element_text(size = base_size*0.8, face = "bold", hjust = 0, color = "white"),  
      legend.position = "right",  
      legend.text.align = NULL,  
      legend.title.align = NULL,  
      legend.direction = "vertical",  
      legend.box = NULL, 
      # Specify panel options
      panel.background = element_rect(fill = "black", color  =  NA),  
      panel.border = element_blank(),  
      panel.grid.major = element_blank(),  
      panel.grid.minor = element_blank(),  
      panel.margin = unit(0.5, "lines"),   

      # Specify plot options
      plot.background = element_rect(color = "black", fill = "black"),  
      plot.title = element_text(size = base_size*1.2, color = "white"),  
      plot.margin = unit(rep(1, 4), "lines")

    )

}

所以下面的代码带有嘈杂的颜色......:

n = 100000

X1 = rnorm(n = n, 0, 1)
X2 = rnorm(n = n, 0, 1)
Y = sin(5*X1*X2+rnorm(n = n, 0, 1))


df = data.frame(X1,X2,Y)

gg <- ggplot(df, aes(x=X1, y=X2)) + 
  geom_point(aes(col=Y), size=1, shape=".") + 
  scale_color_gradient(low="blue", high="red") +
  xlim(c(-5, 5)) + 
  ylim(c(-5, 5)) + 
  labs(title="Scatterplot", 
       caption = "Source: SMAE") +
  theme_black()


plot(gg)

将给出下面的图片。

在此处输入图像描述