2017-09-11 79 views
0

我試圖做一個等高線圖(或平畦,我並不特別在意哪一個),看起來是這樣的:如何向格子中的輪廓圖添加顏色?

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

有一些很好的建議[這裏](https://stackoverflow.com/questions/7851602/methods-for-doing-heatmaps-水平等高線圖和六邊形分檔) –

回答

2

從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

如果不從格子包僅限於期權,filled.contour從grpahics包裝看起來相當不錯。你需要做一些數據的爭論:

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

+0

謝謝!此外,該鏈接是非常有用的,包含很多好的提示。 – rs028