On Tue, 16 Nov 2021 09:45:34 +0100
Luigi Marongiu <marongiu.lu...@gmail.com> wrote:

> contour(df$X, df$Y, df$Z)  

contour() works on matrices (sometimes called "wide format" data). Z
must be a numeric matrix, X must be a numeric vector with length(X) ==
nrow(Z), and Y must be a numeric vector with length(Y) == ncol(Z). This
is described in ?contour.

Since you have a three-column data.frame ("long format" data), you can
use lattice::contourplot instead (e.g. contourplot(Z ~ X + Y, data =
df)) or the appropriate combination of ggplot2 functions.

Alternatively, if your data is already on a grid, you can make a matrix
out of your three-column data.frame, but the procedure is a bit awkward:

ret <- reshape(
 df, direction = "wide", v.names = 'Z', idvar = 'X', timevar = 'Y'
)
contour(
 X <- ret[, 1],
 Y <- attr(ret, "reshapeWide")$times,
 Z <- as.matrix(ret[,-1])
)

(Can be also done with xtabs(); reshape2 and many other contributed
packages also offer some ways of doing that.)

-- 
Best regards,
Ivan

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to