get a surface plot in R

2019-07-25 00:36发布

问题:

I'm trying to obtain a surface plot from data frame AAA:

j     a           m  p       o            f
13929 0.86739583  19 165.83  0.1588727    13.24444
13930 0.63166667  19 178.19  0.6105804    12.68333
13932 0.90212963  17 157.77  0.3345627    12.52222
13933 0.80152778  68 146.19  0.1219885    12.35000
13934 0.75784722  62 134.88  0.1531627    12.36667
13935 0.57763889  66 123.80  0.4093869    12.47500
13936 0.56201389  88 112.87  0.9095722    12.45833
13937 0.51680556  26 102.03  0.8494420    12.37500
13938 0.46093333  28  91.20  0.9153419    12.21111
13939 0.16645833  24  80.30  0.8309784    12.04444
13940 0.15451389  36  69.23  2.2847927    12.15556
13941 0.51347222 134  57.92  2.9551087    12.42500
13942 0.33763889 128  46.31  3.5784096    12.53333
13943 0.12937500  38  34.33  3.7371723    12.47778
13944 0.42760870  63  22.00  4.7831677    12.46667
13945 0.09962121   8   9.36  4.8281897    12.30000
13950 0.97901515  18  57.70  0.0000000    12.15833
13951 0.85333333  14  71.07  0.0000000    12.48333
13952 0.92811594  14  84.28 10.0444672    12.49167
13953 0.84812500  42  97.29  7.8020987    12.51667

My code:

require(fields)
fitx <- Tps( AAA[, 4:6], AAA$a)
out.p <- predict.surface(fitx, xy = c(4,5))
plot.surface(out.p, type="p")

However, it doesn't run through. Apparently, the grid is insufficient to represent the data and it's not able to get the predict.surface.

回答1:

In the Tps function, your x matrix is AAA[, 4:6] and hence has three columns.

But in the predict.surface function, you specified xy = c(4,5). The values passed to the xyparamter are relative to the matrix in your fitx object. Since the matrix used for creating fitx with the predict.surface function has three columns, you can't refer to the 4th and 5th column. Instead, the columns 4 and 5 of your original data.frame AAA correspond to columns 1 and 2 in fitx.

You might wish to try:

library(fields)
fitx <- Tps(AAA[, 4:6], AAA$a)
out.p <- predict.surface(fitx, xy = c(1,2)) # Note the different argument passed to `xy`
plot.surface(out.p, type="p")