如何为晶格中的等值线图添加颜色?

时间:2017-09-11 16:13:02

标签: r plot contour lattice

我正在尝试制作一个等高线图(或水平图,我并不特别关心哪一个)看起来像这样:

enter image description here

我有这段代码:

contourplot(z~x*y, data=df1, xlim=c(0,100), ylim=c(0,50),
            scales=list(x=list(at=c(0,20,40,60,80,100)),
                        y=list(at=c(0,10,20,30,40,50))),
            at=seq(0,5000,by=500))

工作正常,但当我尝试添加这样的颜色时:

contourplot(z~x*y, data=df1, xlim=c(0,100), ylim=c(0,50),
            scales=list(x=list(at=c(0,20,40,60,80,100)),
                        y=list(at=c(0,10,20,30,40,50))),
            at=seq(0,5000,by=500), region=T,
            colorkey=list(at=seq(0,5000,by=10)), col.regions=rainbow(5000))

我得到了这个可怕的结果:

enter image description here

我认为问题主要是规模问题,但无论我为atcol.regions尝试哪种值,我都无法改变结果。我甚至尝试过interpolate选项,但是它会使鼻涕产生影响。我已经搜索了很多,但我不得不说我发现格子的文档有点复杂。

data.frame是这样的:

structure(list(x = c(99.9735523336143, 99.9735523336143, 99.9735523336143, 
9.99735523336143, 99.9735523336143, 99.9735523336143, 99.9735523336143, 
99.9735523336143, 9.99735523336143, 99.9735523336143, 9.99735523336143, 
9.99735523336143, 9.99735523336143, 9.99735523336143, 9.99735523336143, 
9.99735523336143, 19.9947104667229, 19.9947104667229, 19.9947104667229, 
19.9947104667229, 19.9947104667229, 19.9947104667229, 19.9947104667229, 
19.9947104667229, 29.9920657000843, 29.9920657000843, 29.9920657000843, 
29.9920657000843, 29.9920657000843, 29.9920657000843, 29.9920657000843, 
29.9920657000843, 39.9103904572927, 39.9103904572927, 39.9103904572927, 
39.9103904572927, 39.9103904572927, 39.9103904572927, 39.9103904572927, 
39.9103904572927, 49.9867761668072, 49.9867761668072, 49.9867761668072, 
4.99867761668072, 49.9867761668072, 49.9867761668072, 49.9867761668072, 
49.9867761668072, 4.99867761668072, 49.9867761668072, 4.99867761668072, 
4.99867761668072, 4.99867761668072, 4.99867761668072, 4.99867761668072, 
4.99867761668072, 60.0631618763217, 60.0631618763217, 60.0631618763217, 
60.0631618763217, 60.0631618763217, 60.0631618763217, 60.0631618763217, 
60.0631618763217, 69.9419713954535, 69.9419713954535, 69.9419713954535, 
69.9419713954535, 69.9419713954535, 69.9419713954535, 69.9419713954535, 
69.9419713954535, 79.8207809145854, 79.8207809145854, 79.8207809145854, 
79.8207809145854, 79.8207809145854, 79.8207809145854, 79.8207809145854, 
79.8207809145854, 90.0947428144825, 90.0947428144825, 90.0947428144825, 
90.0947428144825, 90.0947428144825, 90.0947428144825, 90.0947428144825, 
90.0947428144825), y = c(0.0999735523336143, 0.499867761668072, 
9.99735523336143, 0.0999735523336143, 0.999735523336143, 19.9947104667229, 
1.99947104667229, 49.9867761668072, 0.499867761668072, 4.99867761668072, 
9.99735523336143, 0.999735523336143, 19.9947104667229, 1.99947104667229, 
49.9867761668072, 4.99867761668072, 0.0999735523336143, 0.499867761668072, 
9.99735523336143, 0.999735523336143, 19.9947104667229, 1.99947104667229, 
49.9867761668072, 4.99867761668072, 0.0999735523336143, 0.499867761668072, 
9.99735523336143, 0.999735523336143, 19.9947104667229, 1.99947104667229, 
49.9867761668072, 4.99867761668072, 0.0999735523336143, 0.499867761668072, 
9.99735523336143, 0.999735523336143, 19.9947104667229, 1.99947104667229, 
49.9867761668072, 4.99867761668072, 0.0999735523336143, 0.499867761668072, 
9.99735523336143, 0.0999735523336143, 0.999735523336143, 19.9947104667229, 
1.99947104667229, 49.9867761668072, 0.499867761668072, 4.99867761668072, 
9.99735523336143, 0.999735523336143, 19.9947104667229, 1.99947104667229, 
49.9867761668072, 4.99867761668072, 0.0999735523336143, 0.499867761668072, 
9.99735523336143, 0.999735523336143, 19.9947104667229, 1.99947104667229, 
49.9867761668072, 4.99867761668072, 0.0999735523336143, 0.499867761668072, 
9.99735523336143, 0.999735523336143, 19.9947104667229, 1.99947104667229, 
49.9867761668072, 4.99867761668072, 0.0999735523336143, 0.499867761668072, 
9.99735523336143, 0.999735523336143, 19.9947104667229, 1.99947104667229, 
49.9867761668072, 4.99867761668072, 0.0999735523336143, 0.499867761668072, 
9.99735523336143, 0.999735523336143, 19.9947104667229, 1.99947104667229, 
49.9867761668072, 4.99867761668072), z = c(7.70262939725884, 
72.4762038498177, 1897.66010932237, 1.58054830233696, 165.070646557113, 
3792.42153530273, 356.278306229365, 9223.47853672023, 14.2758673767137, 
935.839000278543, 341.884101039707, 31.701193606272, 665.004073393329, 
66.9391931173054, 1509.15007815714, 171.754281181158, 2.89064204660586, 
26.2635839730677, 636.27993673818, 58.5156364281774, 1243.02434222159, 
123.885961157754, 2852.46703855471, 318.792649405134, 3.97493187787477, 
36.3111148789109, 889.55438527022, 81.1632049285379, 1744.63858516148, 
172.284250494152, 4044.24262993302, 444.58461882461, 4.86420556288387, 
44.6549593184585, 1105.67331471408, 100.121759918144, 2176.12813241023, 
213.072583938431, 5090.54862806588, 551.33727936112, 5.608738003459, 
51.7306506719939, 1294.26413375778, 0.827199861040891, 116.33438144459, 
2555.52749317869, 248.208540590425, 6028.1869116069, 7.44790194067698, 
643.985400190343, 177.310068151686, 16.5109365887457, 344.080667093976, 
34.8178598080871, 776.206332364582, 89.2039775124962, 6.21939893932657, 
57.6140181031486, 1455.92520463951, 129.934909189257, 2883.12188866332, 
277.918147781392, 6852.31788369484, 722.963381835502, 6.70936909598539, 
62.4057616449996, 1591.87930397262, 141.11465788703, 3160.532277617, 
302.546915494946, 7561.72493964819, 789.007565625702, 7.10911809960051, 
66.3821496606545, 1708.59117356901, 150.482643243694, 3400.26770836962, 
323.370848584576, 8184.13138429934, 845.374097718433, 7.44465742618642, 
69.7893126020086, 1812.39459545251, 158.596014882138, 3614.91792337778, 
341.585243605719, 8749.51748025968, 895.192251986793)), .Names = c("x", 
"y", "z"), row.names = c(NA, -88L), class = "data.frame")

1 个答案:

答案 0 :(得分:3)

从latticeExtra包中添加panel.2dsmoother将为您提供(相对)平滑的颜色。虽然如果仔细观察,边缘仍然有点锯齿状:

contourplot(z~x*y, data=df1, xlim=c(0,100), ylim=c(0,50),
            scales=list(x=list(at=c(0,20,40,60,80,100)),
                        y=list(at=c(0,10,20,30,40,50))),
            at=seq(0,5000,by=500), region=T,
            colorkey=list(at=seq(0,5000,by=10)), 
            col.regions=rainbow(5000),
            panel = latticeExtra::panel.2dsmoother)

contourplot

如果您不受格子包中的选项限制,那么来自grpahics包的filled.contour看起来相当不错。你需要做一些数据争论:

df2 <- tidyr::spread(df1, y, z)
df.x <- df2$x
df.y <- as.numeric(colnames(df2)[-1])
df.z <- as.matrix(df2[,-1])

filled.contour(df.x, df.y, df.z,
               nlevels = 10,
               col = rainbow(10),
               plot.axes = {
                 axis(1)
                 axis(2)
                 contour(df.x, df.y, df.z, add = T)}
               )

filled.contour