2013-08-02 33 views
4

我目前有一個英國的shapefile文件,並繪製了英國不同地區的物種數量圖。到目前爲止,我只繪製了3種物種的種羣,並將它們染成紅色=高,橙色=中等,綠色=低。但是我想要做的是製作一個漸變情節,而不是隻有3種顏色。 到目前爲止,我有一個名爲Count的表格,它具有區域作爲列名稱,然後是下面每個區域的物種數量。我最低的數字是0,最高的數字是2500左右,Count中的區域與shapefile中的區域相匹配。我有一個決定什麼是高,中,低的基礎上水平,你輸入自己R:shapefile上的漸變圖

High<-colnames(Count)[which(Count>'input value here')] 

,然後將這些繪製到shape文件這樣的功能:

plot(ukmap[(ukmap$Region %in% High),],col='red',add=T) 

可惜我不能真的安裝任何軟件包,我想用colorRamp,但我不知道該怎麼辦?

編輯:我的數據看起來是這樣

 Wales Midlands North Scotland South East South West 
1  551  32  124  1   49   28 
3   23  99  291  152  164  107 
4   1  7  17  11   21   14 
7  192  32  12  0   1   9 
9   98  97  5  1   21   0 

和第一列僅僅是一個數字,代表品種,目前我有一個繪製計數到英國shape文件,但基於邊界的功能高,中和低。上面的數據沒有附加到我的shapefile。然後,我循環遍歷數據集的每一行(物種),並繪製每行(物種)的新地圖。

回答

3

OK,這裏不使用ggplot(我會離開,以供參考ggplot溶液)的替代解決方案。這段代碼很簡單,但它應該足以讓您瞭解如何將其應用於自己的數據。

# UK shapefile found via http://www.gadm.org/download 
uk.url <- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip" 

# replace following with your working directory - no trailing slash 
work.dir <- "C:/Temp/r.temp/gb_map" 

# the full file path for storing file 
file.loc <- paste0(work.dir, "/uk.zip") 

download.file (uk.url, destfile = file.loc, mode = "wb") 
unzip(file.loc, exdir = work.dir) 

# open the shapefile 
require(rgdal) 
uk <- readOGR(work.dir, layer = "GBR_adm2") 

# make some fake data to plot 
[email protected]$count <- round(runif(nrow([email protected]), 0, 2500), 0) 
[email protected]$count <- as.numeric([email protected]$count) 

# and plot it 
plot(uk, col = gray([email protected]$count/2500)) 

該代碼的結果如下圖。

screenshot

編輯下面的請求,包括一個傳說,我已經調整了代碼一點點,但在所有誠實,我不明白基礎R的legend功能也足以讓生產質量的東西,我沒有希望進一步研究。 (順便提一下,對於想法,請參考this question)。查看代碼下面的圖表表明我們需要對傳奇顏色等進行重新排序,但我會將其作爲練習留在原始海報中,或作爲另一個問題發佈。

# UK shapefile found via http://www.gadm.org/download 
uk.url <- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip" 

# replace following with your working directory - no trailing slash 
work.dir <- "C:/Temp/r.temp/gb_map" 

# the full file path for storing file 
file.loc <- paste0(work.dir, "/uk.zip") 

download.file (uk.url, destfile = file.loc, mode = "wb") 
unzip(file.loc, exdir = work.dir) 

# open the shapefile 
require(rgdal) 
uk <- readOGR(work.dir, layer = "GBR_adm2") 

# make some fake data to plot 
[email protected]$count <- as.numeric(round(runif(nrow([email protected]), 0, 2500), 0)) 
[email protected]$bin <- cut([email protected]$count, seq(0, 2500, by = 250), 
     include.lowest = TRUE, dig.lab = 4) 

# labels for the legend 
lev = levels([email protected]$bin) 
lev2 <- gsub("\\,", " to ", lev) 
lev3 <- gsub("\\]$", "", lev2) 
lev4 <- gsub("\\(|\\)", " ", lev3) 
lev5 <- gsub("^\\[", " ", lev4) 
my.levels <- lev5 

# Create a function to generate a continuous color palette 
rbPal <- colorRampPalette(c('red','blue')) 
[email protected]$Col <- rbPal(10)[as.numeric(cut([email protected]$count, seq(0, 2500, by = 250)))] 

# Plot 
plot(uk, col = [email protected]$Col) 
legend("topleft", fill = [email protected]$Col, legend = my.levels, col = [email protected]$Col) 

screenshot

+0

真是太棒了,謝謝!你知道如何創建相應的圖例嗎? – userk

+0

簡短的回答是'不是真的'。見上面編輯的答案。 – SlowLearner

0

你試過colorRampPalette了嗎?

這裏是你可以嘗試建立一個梯度調色板

 gradient_color <- colorRampPalette(c("blue", "red")) 
     gradient_color(10) 

[1] 「#0000FF」 「#1C00E2」 「#3800C6」, 「#慄林裏莉」 「#71008D」 「#8D0071」「 #AA0055" [8] 「#C60038」 「#E2001C」 「#FF0000」

的示例圖

 plot(rep(1,10),col=gradient_color(10)) 
+0

一個 「L」,兩個 「T」 秒。 – mdsumner

+0

是的,我想使用colorRampPalette,沿着gradient_color <-colorRampPalette(c(「綠色」,「黃色」,「紅色」))行的東西類似於你已經做了,但我不知道如何把它應用到我的伯爵值,任何想法?非常感謝! – userk

+1

那麼,你沒有給我們一個可重複的問題([詳情請看這裏](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example))這很難幫助。話雖如此,把shapefile放入問題通常並不容易(儘管你可以提供一個下載鏈接),但至少告訴我們你的數據是什麼樣的,然後我們可能會提供幫助。 – SlowLearner

6

好吧,我會咬。我不打算使用base R,因爲plot太難理解了,所以我們將使用ggplot2

# UK shapefile found via http://www.gadm.org/download 
uk.url <- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip" 

# replace following with your working directory - no trailing slash 
work.dir <- "C:/Temp/r.temp/gb_map" 

# the full file path for storing file 
file.loc <- paste0(work.dir, "/uk.zip") 

download.file (uk.url, destfile = file.loc, mode = "wb") 
unzip(file.loc, exdir = work.dir) 

# open the shapefile 
require(rgdal) 
require(ggplot2) 
uk <- readOGR(work.dir, layer = "GBR_adm2") 

# use the NAME_2 field (representing counties) to create data frame 
uk.map <- fortify(uk, region = "NAME_2") 

# create fake count data... 
uk.map$count <- round(runif(nrow(uk.map), 0, 2500), 0) 

# quick visual check 
ggplot(uk.map, aes(x = long, y = lat, group = group, fill = count)) + 
    geom_polygon(colour = "black", size = 0.5, aes(group = group)) + 
    theme() 

這產生了下面的輸出,它可能類似於你所需要的。

screenshot

請注意,我們沒有顯式地指定梯度在這種情況下 - 我們只是把它留給ggplot。如果你想指定這些細節,可能會涉及更多。如果沿着這條路線走,你應該在uk.map中創建另一個列,使用cut函數將每個計數分配到(比方說)10個分箱之一中。該uk.map數據幀是這樣的:

> str(uk.map) 
'data.frame': 427339 obs. of 8 variables: 
$ long : num -2.05 -2.05 -2.05 -2.05 -2.05 ... 
$ lat : num 57.2 57.2 57.2 57.2 57.2 ... 
$ order: int 1 2 3 4 5 6 7 8 9 10 ... 
$ hole : logi FALSE FALSE FALSE FALSE FALSE FALSE ... 
$ piece: Factor w/ 234 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ... 
$ group: Factor w/ 1136 levels "Aberdeen.1","Aberdeenshire.1",..: 1 1 1 1 1 1 1 1 1 1 ... 
$ id : chr "Aberdeen" "Aberdeen" "Aberdeen" "Aberdeen" ... 
$ count: num 1549 1375 433 427 1282 ... 
> 
+0

非常感謝您的幫助,不幸的是,正如我在我的問題中所說的,我無法安裝任何軟件包,但我相信這將對其他人也有用處。 – userk

+0

你既沒有'ggplot2'也沒有'maps'軟件包?你做了一個'library()'調用來檢查? – SlowLearner

+0

我沒有ggplot2,我有gplots,但是我沒有安裝perl。繪製地圖我使用的是maptools,但是我也有地圖軟件包,用於讀取我使用XLConnect的Excel電子表格中的數據。 – userk