2016-12-04 43 views
1

我有我的情節和傳說正確的顏色範圍的問題。spplot問題與傳說範圍和顏色分佈

這是代碼使用:

data.ch4 <- read.csv2("v42_CH4_1970_TOT.txt",skip = 3,stringsAsFactors = FALSE, header = F) 
num_data <- data.frame(data.matrix(data.ch4)) 

library(maptools) 
library(lattice) 
library(png) 

#map loading 
map1 <- readShapePoly("CNTR_2014_03M_SH/Data/CNTR_RG_03M_2014.shp") 
coordinates(num_data) <- ~V2+V1 
gridded(num_data) <- TRUE 

#plotting 
png(file="Map2.png",width=35,height=30,unit="cm", res=200, type = "cairo") 

spplot(num_data["V3"], xlim=c(-5,35), ylim=c(35,70), 
      sp.layout = list("sp.polygons",map1),contour=F) 
dev.off() 

這裏是與數據的文件:https://www.sendspace.com/file/hjtatp(壓縮怎麼一回事,因爲通常它的權重57 mb)個

地圖從here(但地圖具有次級優先級,它可以跳過)

這是怎麼看起來像沒有任何尺度變更: enter image description here

所以一切都是藍色的。很顯然,從最小值到最大值需要很大的距離。我想修復縮尺,例如最後的值將會「高於x」。我試圖做到這一點是這樣的:

enter image description here

所以現在這看起來好多了。這是我是如何做到的:

#Fixed breakpoints (?) 
at <- c(0e+0, 1.5e-5, 1.0e-4, 1.0e-3, 1.0e-2, 1.0e-1, 1.0e+0, 2.0e+0, 1.0e+1, 1.0e+2, 2.0e+2,5.0e+2) 

spplot(num_data["V3"], xlim=c(-5,35), ylim=c(35,70), 
      sp.layout = list("sp.polygons",map1), 
      contour=F, 
      at=at) #right there 

所以我手動添加at值(但不準確的比例)。一切看起來好多了,但.. ..

正如你所看到的,右邊的比例不是均勻分佈的。我看不到任何藍紫色,只有橙色和黃色。

地圖上的一些地方也是亮黃色(德國地區),因爲這裏的價值是最高的,但可悲的是,這個比例上沒有這樣的顏色。

也許我沒有做好。我不知道如何設置比例看起來不錯。我想有規模是這樣的:

enter image description here

我加入實現了這個:

spplot(num_data["V3"], xlim=c(-5,35), ylim=c(35,70), 
      sp.layout = list("sp.polygons",map1), 
      contour=F,at=at, 
      colorkey=list(at=seq(0, 400, 30)) #right there 
      ) 

但同樣,這只是假的規模,它不會工作。

第二個快速問題:如何將國家等值線添加到插入數據的頂部?因爲現在輪廓在顏色豐富的數據下隱藏起來:c

回答

3

轉換爲因子的數據會爲圖例提供固定間隔。你可以通過colorkey = list(labels = list(at = ..., labels = ...))更改標籤及其位置。

[Edited; (I noticed that some values are over 500, sorry)] 

## convert numeric to factor 
[email protected]$cutV3 <- cut([email protected]$V3, breaks = c(at, Inf)) # I modified the breaks 

spplot(num_data["cutV3"], xlim=c(-5, 35), ylim=c(35, 70), 
    colorkey = list(height = 1, labels = list(at = seq(0.5, length(at) -0.5), labels = at)), 
    sp.layout = list("sp.polygons", map1, first = F), contour = F) # drawn after main plot 

enter image description here

+0

真棒!這正是我需要的!非常感謝:) – Karmel

+0

還有一件事,是否有可能使從深藍色到黃色到紅色(藍色 - 黃色 - 紅色)的比例顏色?我正在考慮彩虹模式,但我只能使用綠色 - 黃色 - 紅色或藍色 - 粉紅色 - – Karmel

+1

@Karmel; 'spplot(...,col.regions = colorRampPalette(c(「blue」,「yellow」,「red」)),...)'是你想要的。 – cuttlefish44