[英]3D scatter plot in real time
我正在R中工作,並具有以下形式的數據。 第一列是一個人的生日,第二列是年齡,第三列是死亡原因(自然/疾病/事故),第四列是種族,第五列是出生時的體重,第六列是出生時的長度。
最終,我想創建一個簡短的視頻/時間流逝的3D圖,最后三列為空間尺寸。 視頻播放時間對應於收集我的數據的總時間。 視頻播放時,點會以黑點的形式彈出到其適當的3D位置,然后根據其死因變成藍色/綠色/紅色。
在追求這個想法時,我遇到了這個問題 ,但是它似乎是在實時地移動軸,而不是實時地填充圖形。 我擔心這從根本上是不同的任務。 我還看到jfreechart.com對此很有用,但我更喜歡在R / Matlab中在可能的情況下使用其他軟件來完成此操作。 最后,我願意使用/學習實現此目的所需的任何軟件。
感謝您抽出寶貴的時間!
這可以使用R包scatterplot3d
和animation
來完成。 后者需要安裝ImageMagick 。
這是一些代碼,可以完成您想要執行的操作。
require(animation)
require(scatterplot3d)
# Get some example data
n <- 10
dt <- data.frame(birth = rnorm(n, 50, 20),
age = sample(1:100, n, replace=TRUE),
cause = sample(1:3, n, replace=TRUE),
race = sample(1:5, n, replace=TRUE),
bweight = rnorm(n, 1000, 200),
blen = rnorm(n, 300, 20))
# Starting and final timepoint
st <- 1
ft <- 150
# All the timepoints to evaluate
times <- st:ft
# Matrices that show for each timepoint whether a person is alive or dead.
born <- outer(dt$birth, times, "<")
dead <- outer(dt$birth + dt$age, times, "<")
# Matrix is 0 for not yet born, 1 for living, 2 for dead.
status <- born + dead
# If dead, this contains the status of the cause.
deadcause <- matrix(dt$cause, nc=length(times), nrow=n) * dead + born
# Function to get animations
anim <- function(dt, times) {
for(i in seq_along(times)) {
# Remove those not yet born.
dtcur <- dt * born[, i]
scatterplot3d(x=dtcur$race,
y=dtcur$bweight,
z=dtcur$blen,
color=deadcause[, i],
angle = 135, type = "h",
main=paste("At time", i),
xlim=c(0,5),
ylim=c(0,1500),
zlim=c(0,500))
animation::ani.pause()
}
}
# Save the gif to current directory.
saveGIF(expr = anim(dt, times), interval=.1, outdir = getwd())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.