用R中的数据点绘制四面体 [英] Plotting tetrahedron with data points in R
问题描述
此刻我有点痛苦。
我正在寻找一种绘制成分数据的方法。(
在这里,您可以通过使用<$ c $而无需专用软件包来执行此操作c>几何和 plot3D
。使用您提供的数据:
#加载测试数据
df<-读取.csv( test.csv)[,c(谋杀,抢劫,强奸,加重攻击)]
#将绝对数据转换为相对
df< ;-t(apply(df,1,function(x)x / sum(x)))
#根据https://mathoverflow.net/a/184585 $ b $计算四面体坐标b单形<-函数(n){
qr.Q(qr(matrix(1,nrow = n)),complete = TRUE)[,-1]
}
tetra < ;-simplex(4)
#将重心坐标(4D)转换为笛卡尔坐标(3D)
库(几何)
df3D<-bary2cart(tetra,df)
#绘图数据
库(plot3D)
scatter3D(df3D [,1],df3D [,2],df3D [,3],
xlim = range( tetra [,1]),ylim =范围(tetra [,2]),zlim =范围(tetra [,3]),
col = blue,pch = 16,box = FALSE,theta = 120 )
lines3D(tetra [c(1,2,3,4,1,3,1,2,4),1],
tetra [c(1,2,3,4,1 ,3,1,2,4),2],
tetra [c(1,2,3,4,1,3,1,2,4),3],
col = grey,add = TRUE)
text3D(tetra [,1 ],tetra [,2],tetra [,3],
colnames(df),add = TRUE)
您可以在 scatter3D $ c中使用
phi
和 theta
参数调整方向$ c>。
I'm in a little bit of pain at the moment.
I'm looking for a way to plot compositional data.(https://en.wikipedia.org/wiki/Compositional_data). I have four categories so data must be representable in a 3d simplex ( since one category is always 1 minus the sum of others).
So I have to plot a tetrahedron (edges will be my four categories) that contains my data points.
I've found this github https://gist.github.com/rmaia/5439815 but the use of pavo
package(tcs, vismodel...) is pretty obscure to me.
I've also found something else in composition package, with function plot3D. But in this case an RGL device is open(?!) and I don't really need a rotating plot but just a static plot, since I want to save as an image and insert into my thesis.
Update: data looks like this. Consider only columns violent_crime (total), rape, murder, robbery, aggravated_assault
[ cities violent_crime murder rape rape(legally revised) robbery
1 Autauga 68 2 8 NA 6
2 Baldwin 98 0 4 NA 18
3 Barbour 17 2 2 NA 2
4 Bibb 4 0 1 NA 0
5 Blount 90 0 6 NA 1
6 Bullock 15 0 0 NA 3
7 Butler 44 1 7 NA 4
8 Calhoun 15 0 3 NA 1
9 Chambers 4 0 0 NA 2
10 Cherokee 49 2 8 NA 2
aggravated_assault
1 52
2 76
3 11
4 3
5 83
6 12
7 32
8 11
9 2
10 37
Update: my final plot with composition package
Here is how you can do this without a dedicated package by using geometry
and plot3D
. Using the data you provided:
# Load test data
df <- read.csv("test.csv")[, c("murder", "robbery", "rape", "aggravated_assault")]
# Convert absolute data to relative
df <- t(apply(df, 1, function(x) x / sum(x)))
# Compute tetrahedron coordinates according to https://mathoverflow.net/a/184585
simplex <- function(n) {
qr.Q(qr(matrix(1, nrow=n)) ,complete = TRUE)[,-1]
}
tetra <- simplex(4)
# Convert barycentric coordinates (4D) to cartesian coordinates (3D)
library(geometry)
df3D <- bary2cart(tetra, df)
# Plot data
library(plot3D)
scatter3D(df3D[,1], df3D[,2], df3D[,3],
xlim = range(tetra[,1]), ylim = range(tetra[,2]), zlim = range(tetra[,3]),
col = "blue", pch = 16, box = FALSE, theta = 120)
lines3D(tetra[c(1,2,3,4,1,3,1,2,4),1],
tetra[c(1,2,3,4,1,3,1,2,4),2],
tetra[c(1,2,3,4,1,3,1,2,4),3],
col = "grey", add = TRUE)
text3D(tetra[,1], tetra[,2], tetra[,3],
colnames(df), add = TRUE)
You can tweak the orientation with the phi
and theta
arguments in scatter3D
.
这篇关于用R中的数据点绘制四面体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!