0

I have code below that works where it plots a 3D scatterlplot using scatterplot3d. However, I would like to switch the scale of the the z axis "Count" around so that the max value is at the origin. How do I edit the code so that I can plot data points where the z axis has a max count at the origin?

   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)


}

Expected results would have points plotted where the z axis (count) is max (2000) at the origin and min (13) at the top of the axis. X and y axis range from 0 (origin) to 1 as it is written in this script.

2 Answers 2

0

I don't think there is an option in scatterplot3d::scatterplot3d() to do that directly so you will have to take a more manual approach by reversing the z-values yourself and using the z.ticklabs argument to provide a corresponding reverse labeling.

Here is a simple example. First generate three points and make the plot using defaults:

x <- rep(0, 3)
y <- rep(0, 3)
z <- c(0, 1, 10)

scatterplot3d(x, y, z)

enter image description here

Then reverse the z-values and provide desired labeling:

z_rev <- max(z) - z
scatterplot3d(x, y, z_rev, 
              zlab = "z", 
              z.ticklabs = seq(max(z_rev), min(z_rev), -2))

enter image description here

1
  • Thank you Mathew for your input. I've been trying to apply your code but can't seem to get it to reverse. Would you be able to assist please based on the df mentioned above? I'm still new to this.
    – AlexP
    Jul 10, 2019 at 23:23
0

I'm not really sure what you are trying to do, but using your code it would be something like this:

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)
}
1
  • Thanks for taking the time to add that in my code. I don't have R in front of me at the moment but I'll try running it first thing tomorrow. Thanks again!
    – AlexP
    Jul 11, 2019 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.