简体   繁体   中英

R 3D Ellipsoid Graph- How to change axis limits?

I am trying to make a 3D plot showing the relationship between x, y, and z, based on a sample program I found. However, as you can see in the picture, all the x and y points are clumped together. I need the x and y limits to be from about .35 to.5 and from .275 to .4, respectively. I think the problem may be from this step:

Cov <- matrix(c(3,3.5,0,3.5,10,0,0,0,1), 3,3)

I am not exactly sure what this line does.

Any help fixing this issue would be much appreciated.

data <- read.table(textConnection(
  'x     y     z
  0.461 0.348   5.42
  0.429 0.343   4.99
  0.457 0.336   5.22
  0.402 0.332   4.5
  0.438 0.331   4.66
  0.426 0.33    4.69
  0.43  0.329   4.83
  0.398 0.329   4.41
  0.43  0.326   4.74
  0.426 0.326   4.71
  0.442 0.325   4.81
  0.433 0.322   4.72
  0.407 0.322   4.14
  0.405 0.322   4.43
  0.394 0.322   4.07
  0.384 0.321   4.03
  0.432 0.32    4.64
  0.417 0.319   4.47
  0.409 0.319   4.48
  0.443 0.317   4.59
  0.41  0.317   4.23
  0.417 0.316   4.14
  0.421 0.316   4.46
  0.408 0.316   4.42
  0.405 0.315   4.2
  0.4   0.312   4.17
  0.426 0.307   4.15
  0.395 0.304   4.03
  0.384 0.301   3.77
  0.39  0.299   4.23
  0.414 0.324   4.44
  0.359 0.314   3.54
  0.421 0.307   4.4
  0.415 0.325   4.62
  0.398 0.321   4.25
  0.38  0.306   3.84
  0.394 0.312   3.95
  0.401 0.325   4.16
  0.432 0.315   4.55
  0.42  0.328   4.28
  0.437 0.315   4.5
  0.412 0.322   4.47
  0.396 0.307   4.08
  0.413 0.326   4.12
  0.384 0.31    3.78
  0.393 0.307   4.04
  0.399 0.305   4.3
  0.4   0.312   4.22
  0.421 0.323   4.72
  0.395 0.312   4.28
  0.382 0.303   3.86
  0.396 0.323   4.3
  0.385 0.3     4.01
  0.411 0.311   4.05
  0.406 0.326   4.3
  0.394 0.321   3.99
  0.406 0.314   3.98
  0.413 0.325   4.64
  0.457 0.34    5.5
  0.403 0.321   4.34
  0.376 0.302   3.8
  0.36  0.305   3.54
  0.422 0.311   4.35
  0.369 0.316   3.91
  0.385 0.3     3.79
  0.398 0.31    4.07
  0.365 0.296   3.67
  0.389 0.317   4.13
  0.445 0.327   4.66
  0.426 0.331   4.67
  0.383 0.309   3.88
  0.376 0.314   4.02
  0.406 0.322   4.77
  0.406 0.333   4.43
  0.378 0.317   3.98
  0.397 0.311   4.01
  0.389 0.324   4.41
  0.364 0.308   3.88
  0.38  0.307   3.91
  0.381 0.32    4.5
  0.363 0.302   3.82
  0.404 0.33    4.21
  0.342 0.292   3.3
  0.376 0.3     3.91
  0.388 0.311   4.1
  0.369 0.32    3.82
  0.367 0.317   3.78
  0.375 0.314   3.93
  0.414 0.323   4.46
  0.393 0.321   4.23
  0.391 0.323   4.23
  0.402 0.321   4.25
  0.431 0.313   4.6
  0.446 0.349   5.27
  0.392 0.3     3.72
  0.378 0.302   3.69
  0.391 0.327   4.31
  0.41  0.327   4.6
  0.418 0.323   4.36
  0.434 0.346   4.91
  0.375 0.299   3.77
  0.379 0.315   4
  0.414 0.329   4.52
  0.396 0.326   4.01
  0.335 0.293   3.17
  0.398 0.311   3.95
  0.38  0.312   3.79
  0.366 0.306   3.82
  0.376 0.307   4.01
  0.419 0.327   4.73
  0.384 0.306   3.77
  0.396 0.313   3.91
  0.378 0.308   3.81
  0.39  0.306   3.85
  0.381 0.32    3.88
  0.401 0.332   4.83
  0.408 0.329   4.29
  0.412 0.323   4.48
  0.411 0.318   4.4
  0.398 0.313   4.05
  0.418 0.328   4.53
  0.389 0.32    4.32
  0.417 0.311   4.4
  0.415 0.315   4.53
  0.378 0.302   3.78
  0.422 0.318   4.62
  0.411 0.315   4.13
  0.381 0.324   4.12
  0.436 0.33    4.68
  0.422 0.335   4.48
  0.371 0.302   3.6
  0.4   0.317   4.17
  0.433 0.332   4.73
  0.374 0.317   3.93
  0.382 0.308   3.76
  0.437 0.325   4.79
  0.39  0.325   4.33
  0.386 0.316   4.01
  0.453 0.337   4.96
  0.404 0.31    4.4
  0.4   0.317   4.22
  0.395 0.304   4.02
  0.38  0.319   4.02
  0.369 0.296   3.82
  0.397 0.327   4.43
  0.421 0.338   4.72
  0.394 0.317   4.3
  0.446 0.334   4.99
  0.407 0.309   4.42
  0.428 0.322   4.51
  0.413 0.322   4.51
  0.387 0.308   3.96
  0.413 0.316   4.37
  0.461 0.349   5.4
  0.401 0.314   4.04
  0.388 0.319   4.04
  0.408 0.326   4.54
  0.396 0.317   4.35
  0.41  0.329   4.54
  0.434 0.34    4.86
  0.388 0.318   3.86
  0.374 0.311   3.8
  0.415 0.329   4.51
  0.402 0.313   4.12
  0.375 0.322   4
  0.425 0.325   4.45
  0.36  0.306   3.82
  0.391 0.335   4.43
  0.444 0.343   5.35
  0.369 0.311   3.98
  0.395 0.323   4.4
  0.368 0.309   3.77
  0.349 0.305   3.66
  0.348 0.292   3.43
  0.368 0.303   3.52
  0.425 0.341   4.7
  0.402 0.322   4.36
  0.46  0.34    5.28
  0.413 0.317   4.59
  0.383 0.309   3.88
  0.416 0.325   4.4
  0.401 0.339   4.56
  0.386 0.316   3.78
  0.451 0.339   5.05
  0.401 0.32    4.23
  0.42  0.332   4.64
  0.436 0.338   4.88
  0.378 0.322   3.99
  0.425 0.336   4.75
  0.415 0.335   4.64
  0.403 0.321   4.44
  0.362 0.303   3.77
  0.399 0.331   4.17
  0.39  0.311   4.2
  0.379 0.322   4.12
  0.424 0.335   4.63
  0.422 0.341   4.82
  0.383 0.314   4.05
  0.436 0.35    5.3
  0.378 0.324   4.09
  0.413 0.332   4.77
  0.373 0.304   3.62
  0.371 0.317   4.1
  0.339 0.298   3.17
  0.408 0.321   4.3
  0.402 0.332   4.54
  0.403 0.333   4.95
  0.419 0.338   4.86
  0.454 0.312   4.66
  0.39  0.318   4.04'), header = TRUE)

Mean <- c(0.402,0.319, 4.279)
Mean

Cov <- matrix(c(3,3.5,0,3.5,10,0,0,0,1), 3,3)
Cov


round(var(data),2)
round(Cov - var(data),2)


options(rgl.printRglwidget = TRUE)
open3d()
plot3d(data, box=TRUE,
       xlab="x", ylab="y", zlab="z")
aspect3d("iso")
dataMean <- colMeans(data)
dataCov <- var(data)
plot3d( ellipse3d(Cov,centre=Mean, level=.9),
        col="cyan", alpha=0.5, add = TRUE)][1]][1]

在此处输入图片说明

The Cov <- line is defining a covariance matrix. I'm assuming these values are leftover from the script you found? I notice that the Mean vector matches the column means of your dataset ( colMeans(data) ), so I'm hoping that all that needs to happen is to update the Cov matrix.

The following appeared to work for me (updated to dataCov and took out the aspect3d("iso") line) - hope it works on your machine too. BTW - there was a typo in one of your square brackets.

options(rgl.printRglwidget = TRUE)
open3d()
plot3d(data, box=TRUE,
   xlab="x", ylab="y", zlab="z")
# aspect3d("iso")  ## this makes it long and skinny and ugly
dataMean <- colMeans(data)
dataCov <- var(data)
plot3d(ellipse3d(dataCov,centre=Mean, level=.9),
    col="cyan", alpha=0.5, add = TRUE)[[1]][1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM