【问题标题】:Visualization of correlated elements of 2 data frames2 个数据帧的相关元素的可视化
【发布时间】:2021-05-23 10:36:41
【问题描述】:

我正在对几何数据进行一些统计分析。我有两个包含 79 个元素和 3 个特征(x、y、z)的数据框,我发现这两个数据框的不同元素之间的相关性最好。例如,数据框 A 的元素 N°1 的 x,y,z 与数据框 B 的元素 N°15 的 x,y,z 具有良好的相关性。现在,我想要对这些相关性进行 3D 可视化。现在,我有两个带有 ggplot 的 plot3d() 的 3D 图。但我希望它们在一个图中(结合 2 个图)并在相互关联的元素之间画一条线。我的意思是把我的实际情节放在一起,并用线条连接相关元素。
您能否帮我解决 R(最好)甚至 Pyhon 中的解决方案。

我下面的代码是用来绘制的:

# plot of the first dataset
    plot3d( 
      x=matrix_simp[,1], y=matrix_simp[,2], z=matrix_simp[,3], 
      col = mycolors, 
      type = 's', 
      radius = .02,
      xlab="x", ylab="y", zlab="z")
    
    # plot of the second dataset
    plot3d( 
      x=livewire_corps$x, y=livewire_corps$y, z=livewire_corps$z, 
      col = 'royalblue1', 
      type = 's', 
      radius = .02,
      xlab="x", ylab="y", zlab="z")

我的第一个数据集是这样的(我把它转换成ma矩阵)

           [,1]     [,2]     [,3]
 [1,]  0.057479 0.512632 0.423109
 [2,] -0.149431 0.455704 0.174755
 [3,] -0.217623 0.315060 0.280973
 [4,] -0.012548 0.408923 0.431337
 [5,]  0.037205 0.726578 0.300998
 [6,] -0.198987 0.301178 0.333882
 [7,]  0.012198 0.438064 0.428900
 [8,] -0.007949 0.670635 0.201780
 [9,]  0.072704 0.586991 0.412674
[10,] -0.191775 0.413161 0.192841

另一个数据框是

           [,1]     [,2]     [,3]
 [1,]  0.184931 0.448481 0.066145
 [2,]  0.184866 0.454394 0.054991
 [3,]  0.184128 0.473572 0.045406
 [4,]  0.180612 0.492263 0.035785
 [5,]  0.173571 0.511862 0.034852
 [6,]  0.162665 0.526397 0.033614
 [7,]  0.151182 0.539169 0.031943
 [8,]  0.140002 0.553121 0.028965
 [9,]  0.129629 0.569350 0.025923
[10,]  0.119663 0.580098 0.037508

【问题讨论】:

  • 你能提供你使用的代码和数据吗?
  • @slamballais 我已经编辑了我的问题,希望它会更清楚。
  • 请添加示例数据集。
  • .. 也许这些例子可以帮助你入门r-graph-gallery.com/3d_scatter_plot.html
  • 知道了。谢谢。也许您可以详细说明您的问题,以便我们有a complete minimal reproducible example,即可以轻松访问示例数据,以解决您的问题。我们可以从中工作并用来向您展示如何回答您的问题。

标签: python r ggplot2


【解决方案1】:

我仍然不明白您的问题中的“相关”是什么意思,因此以下内容可能只能回答部分问题。但它展示了一种可能的方式如何提供可重现的数据以及如何组合和连接包“rgl”中的点。

library("rgl")

A <- matrix(c(
   0.057479, -0.149431, -0.217623,
  -0.012548,  0.037205, -0.198987,
   0.012198, -0.007949,  0.072704,
  -0.191775,  0.512632,  0.455704,
   0.315060,  0.408923,  0.726578,
   0.301178,  0.438064,  0.670635,
   0.586991,  0.413161,  0.423109,
   0.174755,  0.280973,  0.431337,
   0.300998,  0.333882,  0.4289,
   0.201780,  0.412674,  0.192841), ncol=3)

B <- matrix(c(
  0.184931, 0.448481, 0.066145,
  0.184866, 0.454394, 0.054991,
  0.184128, 0.473572, 0.045406,
  0.180612, 0.492263, 0.035785,
  0.173571, 0.511862, 0.034852,
  0.162665, 0.526397, 0.033614,
  0.151182, 0.539169, 0.031943,
  0.140002, 0.553121, 0.028965,
  0.129629, 0.569350, 0.025923,
  0.119663, 0.580098, 0.037508), ncol = 3)

# creates 3D plot
plot3d(x = A[, 1], y = A[, 2], z = A[, 3],
  type = 's',  col = "blue",  radius = .01,
  xlim = c(-0.3, 0.5),
  ylim = c( 0.3, 0.8),
  zlim = c( 0,   0.6),
  xlab = "x", ylab = "y", zlab = "z")

# adds points
plot3d(x = B[, 1], y = B[, 2], z = B[, 3],
       type = 's',  col = "green",
       radius = .01, add = TRUE)

# connects points
segments3d(x = rbind(A[, 1], B[, 1]), 
           y = rbind(A[, 2], B[, 2]), 
           z = rbind(A[, 3], B[, 3]))

【讨论】:

  • 谢谢一百万。这正是我所寻找的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
  • 2016-05-16
  • 1970-01-01
  • 2020-02-06
相关资源
最近更新 更多