I want to plot a wireframe in R using the lattice package. However, I get the following error message “error using packet 1 negative length vectors are not allowed”. The data looks like the following:
> result_mean
experiment alpha beta packet
1 0 1.0 1 3.000000
2 0 1.1 1 2.571429
The command to create the data is the following
png(file=paste("foobar.png"),width=1280, height=1280);
plot <- wireframe(result_mean$packet ~ result_mean$alpha * result_mean$beta,
data=result_mean, scales = list(arrows=FALSE, cex= .45, col = "black", font = 3),
drape = TRUE, colorkey = TRUE, main = "Foo",
col.regions = terrain.colors(100),
screen = list(z = -60, x = -60),
xlab="alpha", ylab="beta", zlab="mean \npackets");
print(plot);
dev.off();
I’m wondering what this error message means and if there is a good way to debug this?
Thanks in advance!
Debugging lattice graphics is a bit difficult because (a) the code is complex and multi-layered and (b) the errors get trapped in a way that makes them hard to intercept. However, you can at least get some way in diagnosing the problem.
First create a minimal example. I suspected that your problem was that your data fall on a single line, so I created data that looked like that:
Now confirm that fully three-dimensional data (data that define a plane) work just fine:
So the question is really — did you intend to draw a wireframe of two points lying on a line? If so, what did you want to have appear in the plot? You could hack things a little bit to set the
yvalues to differ by a tiny bit — I tried it, though, and got no wireframe appearing (but no error either).edit: I did a bit more tracing, with various
debug()incantations (and searching the source code of thelatticepackage andRitself for “negative length”) to deduce the following: within a function calledlattice:::panel.3dwire, there is a call to a C functionwireframePanelCalculations, which you can see at https://r-forge.r-project.org/scm/viewvc.php/pkg/src/threeDplot.c?view=markup&root=latticeWithin this function:
In this case
nxis zero, so this code is asking R to allocate a negative-length vector, which is where the error comes from.In this case, though, I think the diagnosis is more useful than the explicit debugging.