3

I'm trying to create a 3d scatter plot using the following script:

d <- read.table(file='myfile.dat', header=F)
plot3d(
    d,
    xlim=c(0,20),
    ylim=c(0,20),
    zlim=c(0,10000),
    xlab='Frequency',
    ylab='Size',
    zlab='Number of subgraphs',
    box=F,
    type='s',
    size=0.5,
    col=d[,1]
)
lines3d(
    d,
    xlim=c(2,20),
    ylim=c(0,20),
    zlim=c(0,10000),
    lwd=2,
    col=d[,1]
)
grid3d(side=c('x', 'y+', 'z'))

Now for some reason, R is ignoring the range limits I've specified and is using arbitrary values, messing up my plot. I get no error when I run the script. Does anybody have any idea what's wrong? If required, I can also post an image of the plot that is created. The data file is given below:

myfile.dat

11    2    2
NA    NA    NA
10    2    2
NA    NA    NA
13    2    1
NA    NA    NA
15    2    1
NA    NA    NA
5    2    11
5    3    10
5    4    16
5    5    34
5    6    102
5    7    294
5    8    682
5    9    1439
5    10    2646
5    11    3615
5    12    2844
5    13    1394
NA    NA    NA
4    2    10
4    3    4
4    4    4
4    5    10
4    6    38
4    7    132
4    8    396
4    9    976
4    10    2121
4    11    4085
4    12    6261
4    13    6459
4    14    4238
4    15    1394
NA    NA    NA
7    2    3
NA    NA    NA
6    2    2
NA    NA    NA
9    2    8
9    3    6
9    4    4
9    5    5
NA    NA    NA
8    2    4
8    3    10
8    4    22
8    5    52
8    6    126
8    7    264
8    8    478
8    9    729
8    10    943
8    11    754
8    12    382
NA    NA    NA
IRTFM
  • 258,963
  • 21
  • 364
  • 487
K G
  • 1,715
  • 6
  • 21
  • 29
  • 1
    A reproducible example (with data and code that demonstrates the behavior you see) would be far more useful than an image. I say that because when I run `plot3d` on my computer is responds to `xlim`, etc. changes just fine. – joran Mar 10 '12 at 17:51
  • I'm guessing that your complaint is that `lines3d` is not honoring your limits, but since the regular `line` function does not honor such arguments , shouldn't you think further? Why not use segments3d??? – IRTFM Mar 10 '12 at 19:20
  • @DWin err, that is the code I'm using. What more can I provide you? That's the code giving me the error. And well, I'm providing the ranges to plot3d as well. Why isn't that responding? – K G Mar 10 '12 at 23:18
  • @DWin I just tried just plotting without using lines3d. Same result. x and y both have a range from -20 to 40, which is not what I want at all. – K G Mar 10 '12 at 23:21
  • @DWin ok so apparently if I set the the type to anything besides 's', the ranges are honoured. This is really weird. Anybody know what this is so? – K G Mar 10 '12 at 23:28

2 Answers2

3

The help page, ?plot3d says "Note that since rgl does not currently support clipping, all points will be plotted, and 'xlim', 'ylim', and 'zlim' will only be used to increase the respective ranges." So you need to restrict the data in the input stage. (And you will need to use segments3d instead of lines3d if you only want particular ranges that are inside the plotted volume.)

d2 <- subset(d,  d[,1]>0 & d[,1] <20 & d[,2]>0 & d[,2] <20  & d[,3]>0 & d[,3]<10000 ])
plot3d(
    d2[, 1:3],  # You can probably use something more meaningful,
    xlim=c(0,20),
    ylim=c(0,20),
    zlim=c(0,10000),
    xlab='Frequency',
    ylab='Size',
    zlab='Number of subgraphs',
    box=F,
    type='s',
    size=0.5,
    col=d[,1]
)

(I did notice that when the range was c(0,10000) that the size of the points was pretty much invisible. and further experimentation suggest that the great disparity in ranges is going to cause furhter difficulties in keeping the ranges at 0 on the low side if you increase the size to the point where it is visible. If you make the points really big , they expand the range to accommodate the overlap beyond the x=0 or y=0 planes.)

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Umm, please read my earlier comments. I am not trying to clip the data. The data is already in the ranges I have specified. The problem is that plot3d seems to ignore the ranges when using type 's'. The result is that the plot is too condensed. – K G Mar 10 '12 at 23:58
  • I read all your comments. But you say you get an error but provide neither the error message or any description of the object `d`. It might be saver to use subset on those conditions ,, see if my edit helps. – IRTFM Mar 11 '12 at 00:16
  • err, no. I said I get no error. The data is plotted fine. It's just that the axis ranges are set arbitrarily when I use type 's'. Please note that the ranges are set correctly when I use some other type such as 'p'. Would it help to show you my data? Although I don't think that matters since its plotted fine. – K G Mar 11 '12 at 00:24
  • I think it would help a great deal to include data. – IRTFM Mar 11 '12 at 00:28
  • Ah. I think that explains it then. The spheres are just too big for plot, so it expands the range. Thanks. I guess I'll just use 'p' then. – K G Mar 11 '12 at 00:59
  • I would suggest scaling the z variable so the ranges are not so disproportionate, and then using the 3d axis to label it – IRTFM Mar 11 '12 at 01:55
  • Do you have any suggestion on how I should scale? Doing a logarithmic scale is not an option since it smooth out the distribution and it is crucial that I keep the distribution intact. – K G Mar 11 '12 at 09:20
  • The documentation is not particularly clear on how to add tick-labels but I had a memory of doing previous work on RGL labeling, so maybe this link will be of some assistance: http://stackoverflow.com/questions/8204972/carscatter3d-in-r-labeling-axis-better/8206320#8206320 . If you need more detail I would post another question with data since we have really run out of room for this one. – IRTFM Mar 11 '12 at 16:30
0

As DWin said, lines3d does not handle *lim arguments. From the help page, "... Material properties (see rgl.material), normals and texture coordinates (see rgl.primitive)."
So use some other function, or perhaps you could retrieve the existing limits from your plot3d call and use those to scale your data prior to plotting?

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73