2016-11-01 25 views
1

我有一個SpatialPolygonsDataFrame,其中包含十六進制顏色值的列。我想繪製地圖像這樣與包TMAP:具有tmap中顏色值的列

tm_shape(full.shp) + tm_fill(col="konf1900") 

但後來它被當作範疇變量,造成這樣的: screenshot 2016-11-01 10 15 53

我不知道如何告訴TMAP,它應該直接在地圖上繪製顏色值...

任何人都可以幫忙嗎?

編輯: 請參閱下面的答案 - 問題是數據幀列沒有編碼as.character。我認爲這可能幫助別人有時...

+0

Apperently中,'konf1900'變量包含顏色值。在這種情況下'tm_fill'使用這些值來填充多邊形。據我所知,情節就是這樣。因此,我不清楚你的問題是什麼...... – Jaap

+0

不,不幸的是,它不使用顏色值進行填充 - 例如,比較第一個圖例條目:#08306B這應該是深藍色,不是綠色的。另外,我可以使用palette-command來更改顏色,如果它採用顏色值,則不應該這樣。它將變量視爲分類變量。 – Mario

回答

2

顯然,問題是列的類型:

full.shp$konf1900char <- as.character(full.shp$konf1900) 
tm_shape(full.shp) + tm_fill(col="konf1900char") 

它需要被轉換爲字符as.character。 此外,不存在任何NA值是很重要的,它們可以被轉化爲白色(#FFFFFF十六進制格式):

full.shp$konf1900char[is.na(full.shp$konf1900char)] <- "#ffffff" 

與這些變換,它很好地工作與TMAP和tm_fill需要的色值從變量。

編輯:
這是所得到的圖像(比較該問題截圖以上): enter image description here

0
library(tmap) 

#Load in some example data 
data(Europe) 

#Create a dataframe with all a column containing country ID and another with an assigned color: 
region_col <- data.frame(
    iso_a3 = Europe$iso_a3, 
    mycolors = c(rep("#94b8b8",68)), #Assign the same color to all regions initilally 
    stringsAsFactors = FALSE 
) 

#Highlight some selected regions by looking up the 3 letter iso_a3 code and changing the 
#associated hexidecimal color reference: 
region_col$mycolors[region_col$iso_a3 == "GBR"] <- "#00ff00" 
region_col$mycolors[region_col$iso_a3 == "FRA"] <- "#ff0000" 
region_col$mycolors[region_col$iso_a3 == "DEU"] <- "#ffff00" 
region_col$mycolors[region_col$iso_a3 == "ESP"] <- "#0000ff" 

#Import color selection from region_col dataframe into Europe SpatialPolygonDataFrame 
Europe$color <- region_col$mycolors 

#Plot resultant map: 
qtm(Europe, fill = "color") 

enter image description here

相關問題