如何在 R 中的 3d 绘图中绘制多个 2d 绘图?
How to draw multiple 2d-plots in a 3d-plot in R?
我不确定我表述的 title/question 是否正确。也许我的问题之一是我的词汇中缺少术语。对不起。但是让我们试试:
我有数据(在本例中为 sleep
),我将其描述为三维的。也许真正的统计学家不会那样做?
我想我想把多个二维图画成一个三维图。我想并排绘制它们。如有不妥请指正
我的问题是只有一行。
有两组。我想要每组一行。同样的数据加上type='h'
我觉得描述的更好:
你能想象这里的两条线吗?我在这个概念中缺少什么?
我们可以为 printing/publication 使用另一个绘图库。目前对我来说哪个并不重要。可能我完全走错地方了?
这是代码:
require('mise')
require('scatterplot3d')
mise() # clear the workspace
# example data
print(sleep)
scatterplot3d(x=sleep$ID,
x.ticklabs=levels(sleep$ID),
y=sleep$group,
y.ticklabs=levels(sleep$group),
lab = c(length(unique(sleep$ID)), 1),
z=sleep$extra,
type='o')
和数据
extra group ID
1 0.7 1 1
2 -1.6 1 2
3 -0.2 1 3
4 -1.2 1 4
5 -0.1 1 5
6 3.4 1 6
7 3.7 1 7
8 0.8 1 8
9 0.0 1 9
10 2.0 1 10
11 1.9 2 1
12 0.8 2 2
13 1.1 2 3
14 0.1 2 4
15 -0.1 2 5
16 4.4 2 6
17 5.5 2 7
18 1.6 2 8
19 4.6 2 9
20 3.4 2 10
您可以分两步手动添加这些行:
# Store the plot in rr
rr <- scatterplot3d(x=as.numeric(sleep$ID),
x.ticklabs=levels(sleep$ID),
y=sleep$group,
y.ticklabs=levels(sleep$group),
z=sleep$extra)
# find all that belong to group one
idx = sleep$group == 1
# add the first line
rr$points3d(x = sleep$ID[idx], y = rep(1, each = sum(idx)), z = sleep$extra[idx], type = 'l', col = 'red')
# add the second line
rr$points3d(x = sleep$ID[!idx], y = rep(2, each = sum(!idx)), z = sleep$extra[!idx], type = 'l', col = 'blue')
因此,添加色带而不是线条会有所不同。特别是,色带是使用 polygon
函数绘制的。但是,此函数仅处理 2D 坐标,因此我们需要使用函数 rr$xyz.convert
.
将 3D 坐标转换为 2D 坐标
rr <- scatterplot3d(x=sleep$ID,
x.ticklabs=levels(sleep$ID),
y=sleep$group,
y.ticklabs=levels(sleep$group),
z=sleep$extra)
idx = sleep$group == 1
# draw first group
mat = matrix(c(rep(sleep$ID[idx], 2),
rep(c(1, 1.05), each = sum(idx)), # 1.05 determines width
rep(sleep$extra[idx], 2)), ncol = 3)
ll = rr$xyz.convert(mat)
polygon(x = ll$x[c(1:10, 20:11)],
y = ll$y[c(1:10, 20:11)], col = 'red')
# draw second group
mat = matrix(c(rep(sleep$ID[!idx], 2),
rep(c(2, 1.95), each = sum(!idx)), # 1.95 determines width
rep(sleep$extra[!idx], 2)), ncol = 3)
ll = rr$xyz.convert(mat)
polygon(x = ll$x[c(1:10, 20:11)],
y = ll$y[c(1:10, 20:11)], col = 'blue')
我不确定我表述的 title/question 是否正确。也许我的问题之一是我的词汇中缺少术语。对不起。但是让我们试试:
我有数据(在本例中为 sleep
),我将其描述为三维的。也许真正的统计学家不会那样做?
我想我想把多个二维图画成一个三维图。我想并排绘制它们。如有不妥请指正
我的问题是只有一行。
有两组。我想要每组一行。同样的数据加上type='h'
我觉得描述的更好:
你能想象这里的两条线吗?我在这个概念中缺少什么? 我们可以为 printing/publication 使用另一个绘图库。目前对我来说哪个并不重要。可能我完全走错地方了?
这是代码:
require('mise')
require('scatterplot3d')
mise() # clear the workspace
# example data
print(sleep)
scatterplot3d(x=sleep$ID,
x.ticklabs=levels(sleep$ID),
y=sleep$group,
y.ticklabs=levels(sleep$group),
lab = c(length(unique(sleep$ID)), 1),
z=sleep$extra,
type='o')
和数据
extra group ID
1 0.7 1 1
2 -1.6 1 2
3 -0.2 1 3
4 -1.2 1 4
5 -0.1 1 5
6 3.4 1 6
7 3.7 1 7
8 0.8 1 8
9 0.0 1 9
10 2.0 1 10
11 1.9 2 1
12 0.8 2 2
13 1.1 2 3
14 0.1 2 4
15 -0.1 2 5
16 4.4 2 6
17 5.5 2 7
18 1.6 2 8
19 4.6 2 9
20 3.4 2 10
您可以分两步手动添加这些行:
# Store the plot in rr
rr <- scatterplot3d(x=as.numeric(sleep$ID),
x.ticklabs=levels(sleep$ID),
y=sleep$group,
y.ticklabs=levels(sleep$group),
z=sleep$extra)
# find all that belong to group one
idx = sleep$group == 1
# add the first line
rr$points3d(x = sleep$ID[idx], y = rep(1, each = sum(idx)), z = sleep$extra[idx], type = 'l', col = 'red')
# add the second line
rr$points3d(x = sleep$ID[!idx], y = rep(2, each = sum(!idx)), z = sleep$extra[!idx], type = 'l', col = 'blue')
因此,添加色带而不是线条会有所不同。特别是,色带是使用 polygon
函数绘制的。但是,此函数仅处理 2D 坐标,因此我们需要使用函数 rr$xyz.convert
.
rr <- scatterplot3d(x=sleep$ID,
x.ticklabs=levels(sleep$ID),
y=sleep$group,
y.ticklabs=levels(sleep$group),
z=sleep$extra)
idx = sleep$group == 1
# draw first group
mat = matrix(c(rep(sleep$ID[idx], 2),
rep(c(1, 1.05), each = sum(idx)), # 1.05 determines width
rep(sleep$extra[idx], 2)), ncol = 3)
ll = rr$xyz.convert(mat)
polygon(x = ll$x[c(1:10, 20:11)],
y = ll$y[c(1:10, 20:11)], col = 'red')
# draw second group
mat = matrix(c(rep(sleep$ID[!idx], 2),
rep(c(2, 1.95), each = sum(!idx)), # 1.95 determines width
rep(sleep$extra[!idx], 2)), ncol = 3)
ll = rr$xyz.convert(mat)
polygon(x = ll$x[c(1:10, 20:11)],
y = ll$y[c(1:10, 20:11)], col = 'blue')