我下面的代码可以在使用scatterplot3d绘制3D scatterlplot的地方工作。但是,我想切换z轴“ Count”的比例,以便最大值位于原点。如何编辑代码,以便可以绘制z轴在原点具有最大计数的数据点?
df
ID Height Weight Class Count
1 0.75 0.98 6 13
2 0.62 0.11 2 18
3 0.33 0.13 3 300
4 0.34 0.15 4 43
5 0.67 0.14 5 2000
Height <- APP$Height
Weight <- APP$Weight
Class <- APP$Class
Count <- APP$Count
color <- c(rgb(0, 1, 0), rgb(0.25, 0.75, 0), rgb(0.5, 0.5, 0),
rgb(0.75, 0.25, 0), rgb(1, 0, 0))
scatterplot3d(x=c(0.0, 0.45, 0.45, 0, 0), y=c(0, 0, 0.45, 0.45, 0),
z=c(0, 0, 0, 0, 0), box=T, type='l', color='black', grid=F, lwd=2,
xlab='Height)', ylab='', zlab='Count',
xlim=c(0, 1), ylim=c(0, 1), zlim=c(2260,0), angle=15)
text(7, 0, ' Weight',
srt=11)
par(new=T)
scatterplot3d(x=c(0.0, 0.45, 0.45, 0.0, 0.0), y=c(0.55, 0.55, 1, 1,
0.55), z=rep(0,5), box=F, type='l', color='black', grid=F, lwd=2,
xlab='', ylab='', zlab='', xlim=c(0, 1), ylim=c(0, 1),
zlim=c(0,2260), axis=F, angle=15)
par(new=T)
scatterplot3d(x=c(0.55, 1, 1, 0.55, 0.55), y=c(0.0, 0.0, 0.45, 0.45,
0.0), z=rep(0,5), box=F, type='l', color='black', grid=F, lwd=2,
xlab='', ylab='', zlab='', xlim=c(0, 1), ylim=c(0, 1),
zlim=c(0,2260), axis=F, angle=15)
par(new=T)
scatterplot3d(x=c(0.55, 1, 1, 0.55, 0.55), y=c(0.55, 0.55, 1, 1, 0.55),
z=rep(0,5), box=F, type='l', color='black', grid=F, lwd=2,
xlab='', ylab='', zlab='', xlim=c(0, 1), ylim=c(0, 1),
zlim=c(0,2260), axis=F, angle=15)
par(new=T)
for (i in 6:2) {
scatterplot3d(Height[Class==i], Weight[Class==i], Count[Class==i],box=F,
pch=c(1,1,0,1,20)[i-1], color=color[i-1], grid=F, xlab='', ylab='',
zlab='', xlim=c(0, 1), ylim=c(0, 1),
zlim=c(0, 2260), axis=F, angle=15)
par(new=T)
}
期望的结果将标出点,其中z轴(计数)在原点的最大值(2000),在轴顶部的最小值(13)。按照此脚本编写的方式,X和y轴的范围从0(原点)到1。
答案 0 :(得分:0)
我认为scatterplot3d::scatterplot3d()
中没有直接执行此操作的选项,因此您将不得不采取一种更手动的方法,即自己反转z值并使用z.ticklabs
参数来提供相应的反向标签。
这是一个简单的例子。首先生成三个点,并使用默认值绘制该图:
x <- rep(0, 3)
y <- rep(0, 3)
z <- c(0, 1, 10)
scatterplot3d(x, y, z)
然后反转z值并提供所需的标签:
z_rev <- max(z) - z
scatterplot3d(x, y, z_rev,
zlab = "z",
z.ticklabs = seq(max(z_rev), min(z_rev), -2))
答案 1 :(得分:0)
我不确定您要做什么,但是使用您的代码将是这样的:
library(readr)
library(dplyr)
library(scatterplot3d)
"ID Height Weight Class Count
1 0.75 0.98 6 13
2 0.62 0.11 2 18
3 0.33 0.13 3 300
4 0.34 0.15 4 43
5 0.67 0.14 5 2000" %>% read_table() -> APP
Height <- APP$Height
Weight <- APP$Weight
Class <- APP$Class
Count <- APP$Count
Count_rev <- max(Count) + min(Count) - Count
color <- c(rgb(0, 1, 0),
rgb(0.25, 0.75, 0),
rgb(0.5, 0.5, 0),
rgb(0.75, 0.25, 0),
rgb(1, 0, 0))
scatterplot3d(x=c(0.0, 0.45, 0.45, 0, 0),
y=c(0, 0, 0.45, 0.45, 0),
z=c(13, 13, 13, 13, 13), box=T, type='l', color='black', grid=F, lwd=2,
xlab='Height)', ylab='', zlab='Count',
xlim=c(0, 1), ylim=c(0, 1), zlim=c(13, 2000), angle=15, z.ticklabs = c(2000, 1500, 1000, 500, 13))
text(7, 0, 'Weight', srt=11)
par(new=T)
scatterplot3d(x=c(0.0, 0.45, 0.45, 0.0, 0.0),
y=c(0.55, 0.55, 1, 1, 0.55),
z=rep(13,5), box=F, type='l', color='black', grid=F, lwd=2,
xlab='', ylab='', zlab='', xlim=c(0, 1), ylim=c(0, 1),
zlim=c(13, 2000), axis=F, angle=15, z.ticklabs = c(2000, 1500, 1000, 500, 13))
par(new=T)
scatterplot3d(x=c(0.55, 1, 1, 0.55, 0.55),
y=c(0.0, 0.0, 0.45, 0.45, 0.0),
z=rep(13,5), box=F, type='l', color='black', grid=F, lwd=2,
xlab='', ylab='', zlab='', xlim=c(0, 1), ylim=c(0, 1),
zlim=c(13,2000), axis=F, angle=15, z.ticklabs = c(2000, 1500, 1000, 500, 13))
par(new=T)
scatterplot3d(x=c(0.55, 1, 1, 0.55, 0.55), y=c(0.55, 0.55, 1, 1, 0.55),
z=rep(13,5), box=F, type='l', color='black', grid=F, lwd=2,
xlab='', ylab='', zlab='', xlim=c(0, 1), ylim=c(0, 1),
zlim=c(13,2000), axis=F, angle=15, z.ticklabs = c(2000, 1500, 1000, 500, 13))
par(new=T)
for (i in 6:2) {
scatterplot3d(Height[Class==i], Weight[Class==i], Count_rev[rev(Class==i)],box=F,
pch=c(1,1,0,1,20)[i-1], color=color[i-1], grid=F, xlab='', ylab='',
zlab='', xlim=c(0, 1), ylim=c(0, 1),
zlim=c(13, 2000), axis=F, angle=15, z.ticklabs = c(2000, 1500, 1000, 500, 13)) # z.ticklabs
par(new=T)
}