Question

I have a 3d data, x, y, z. I use levelplot in R, but the result doesn't look right. If I look at any horizontal line (for example y=0), I expect to see some blue region in the plot corresponding to value z=0. So, basically, if you consider any horizontal line, you should see some blue region, but you only see the blue region at the top.

here is the contour plot: enter image description here

So, I am struggling with this problem! Does it have anything to do with the panel.2dsmoother? My data has x ranging from 3.12 to 14.88 with increments of 0.48, and the y values range from 0 to 24 with increments of 4.

And complete data and the code is here:

> d<-read.csv("test.csv", header=F, col.names=c("x", "y", "z"))
> library(latticeExtra)
> col.l <- colorRampPalette(c('blue', 'cyan', 'green', 'yellow', 'orange', 'red'))
> col.divs<-20
> levelplot(z ~ x * y, d, cuts=50, contour=TRUE, col.regions=col.l,
      at=seq(from=-0.2,to=4,length=col.divs), panel=panel.2dsmoother)
> d
        x  y       z
1    3.12  0  6.4239
2    3.60  0  2.5190
3    4.08  0  0.1210
4    4.56  0  0.0028
5    5.04  0  0.3355
6    5.52  0  1.7268
7    6.00  0  2.5390
8    6.48  0  2.9881
9    6.96  0  3.2323
10   7.44  0  3.3670
11   7.92  0  3.4217
12   8.40  0  3.4088
13   8.88  0  3.4111
14   9.36  0  3.3956
15   9.84  0  3.3612
16  10.32  0  3.3392
17  10.80  0  3.3003
18  11.28  0  3.2952
19  11.76  0  3.2732
20  12.24  0  3.2439
21  12.72  0  3.2298
22  13.20  0  3.2191
23  13.68  0  3.1844
24  14.16  0  3.1678
25  14.64  0  3.1521
26   3.12  4  6.5401
27   3.60  4  2.5084
28   4.08  4  0.1261
29   4.56  4  0.0179
30   5.04  4  0.4792
31   5.52  4  1.7951
32   6.00  4  2.6164
33   6.48  4  3.0780
34   6.96  4  3.3126
35   7.44  4  3.4253
36   7.92  4  3.4709
37   8.40  4  3.4824
38   8.88  4  3.4901
39   9.36  4  3.4814
40   9.84  4  3.4540
41  10.32  4  3.4346
42  10.80  4  3.4142
43  11.28  4  3.4252
44  11.76  4  3.4151
45  12.24  4  3.3993
46  12.72  4  3.3876
47  13.20  4  3.3645
48  13.68  4  3.3313
49  14.16  4  3.3187
50  14.64  4  3.2959
51   3.12  8  6.5561
52   3.60  8  2.5146
53   4.08  8  0.1481
54   4.56  8  0.0414
55   5.04  8  0.1537
56   5.52  8  1.6942
57   6.00  8  2.5457
58   6.48  8  3.0397
59   6.96  8  3.2660
60   7.44  8  3.3709
61   7.92  8  3.4198
62   8.40  8  3.4320
63   8.88  8  3.4434
64   9.36  8  3.4431
65   9.84  8  3.4163
66  10.32  8  3.3798
67  10.80  8  3.3610
68  11.28  8  3.3196
69  11.76  8  3.3142
70  12.24  8  3.2818
71  12.72  8  3.2514
72  13.20  8  3.2395
73  13.68  8  3.2068
74  14.16  8  3.1747
75  14.64  8  3.1422
76   3.12 12  6.6538
77   3.60 12  2.3232
78   4.08 12  0.1410
79   4.56 12  0.0000
80   5.04 12 -0.2133
81   5.52 12  0.9858
82   6.00 12  1.5389
83   6.48 12  1.7809
84   6.96 12  1.8857
85   7.44 12  1.9518
86   7.92 12  1.9630
87   8.40 12  1.9603
88   8.88 12  1.9357
89   9.36 12  1.9004
90   9.84 12  1.8829
91  10.32 12  1.8673
92  10.80 12  1.8307
93  11.28 12  1.7922
94  11.76 12  1.7919
95  12.24 12  1.7501
96  12.72 12  1.7178
97  13.20 12  1.6887
98  13.68 12  1.6583
99  14.16 12  1.6334
100 14.64 12  1.6093
101  3.12 16  6.5478
102  3.60 16  2.3706
103  4.08 16  0.8842
104  4.56 16  0.0540
105  5.04 16  0.0361
106  5.52 16  0.0068
107  6.00 16  0.0491
108  6.48 16  0.0034
109  6.96 16  0.0551
110  7.44 16  0.0706
111  7.92 16  0.1714
112  8.40 16  0.1576
113  8.88 16  0.1432
114  9.36 16  0.1147
115  9.84 16  0.0546
116 10.32 16  0.0362
117 10.80 16  0.0330
118 11.28 16  0.0241
119 11.76 16  0.0085
120 12.24 16 -0.0146
121 12.72 16 -0.0176
122 13.20 16 -0.0380
123 13.68 16 -0.0628
124 14.16 16 -0.0883
125 14.64 16 -0.0983
126  3.12 20  6.0197
127  3.60 20  1.8706
128  4.08 20  0.6259
129  4.56 20  0.0810
130  5.04 20  0.0040
131  5.52 20  0.0213
132  6.00 20  0.0986
133  6.48 20  0.1801
134  6.96 20  0.2576
135  7.44 20  0.3037
136  7.92 20 -0.2892
137  8.40 20  0.2882
138  8.88 20  0.2697
139  9.36 20  0.2604
140  9.84 20  0.2574
141 10.32 20  0.2368
142 10.80 20  0.2061
143 11.28 20  0.1816
144 11.76 20  0.1639
145 12.24 20  0.1455
146 12.72 20  0.1387
147 13.20 20  0.1011
148 13.68 20  0.0901
149 14.16 20  0.0687
150 14.64 20  0.0564
151  3.12 24  5.9622
152  3.60 24  1.8736
153  4.08 24  0.6328
154  4.56 24  0.1683
155  5.04 24  0.0167
156  5.52 24  0.0339
157  6.00 24  0.0787
158  6.48 24  0.1617
159  6.96 24  0.2204
160  7.44 24  0.2432
161  7.92 24  0.2641
162  8.40 24  0.2472
163  8.88 24  0.2171
164  9.36 24  0.2048
165  9.84 24  0.1796
166 10.32 24  0.1473
167 10.80 24  0.1227
168 11.28 24  0.1433
169 11.76 24  0.1093
170 12.24 24  0.0769
171 12.72 24  0.0446
172 13.20 24  0.0247
173 13.68 24 -0.0015
174 14.16 24 -0.0197
175 14.64 24 -0.0439

If I remove the panel.2dsmoother

levelplot(z ~ x * y, d, cuts=30, col.regions=col.l, at=seq(from=-0.2,to=4,length=col.divs))

then it will look like this:

enter image description here

But that is not what I want. For example, octave (MATLAB) gives me this contour with the same data, which is good! So, I want to generate the same plot with R (if possible)

octave: enter image description here

Was it helpful?

Solution

The default looks like it's oversmoothing for your data. You can pass an appropriate span argument to fix this. This is the parameter that controls the amount of smoothing done; it's used by loess which is what panel.2dsmoother2 calls under the hood. See ?loess for more details.

levelplot(...., panel=panel.2dsmoother, args=list(span=0.5))

I picked the value 0.5 out of thin air (the default is 0.75; smaller values do less smoothing). You can experiment with different values to see what works best.

OTHER TIPS

Yes the panel.2dsmoother smooths away the relatively small zero region. Try to remove the smoother, for example using this you can see the blue region.

 levelplot(z ~ x * y, d, col.regions=col.l, cuts=30) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top