您需要合并您的数据 df 至 World 这是一个 SpatialPolygonsDataFrame 。之后你就可以了 1950 列作为的一部分 World 多边形。
df
World
SpatialPolygonsDataFrame
1950
的 编辑 强> :正如RobertH在评论中指出的那样,最好使用它 World <- merge(World, df, by = "name")
World <- merge(World, df, by = "name")
library(tmap) data(World) str(World, max.level = 2) #> Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots #> ..@ data :'data.frame': 177 obs. of 15 variables: #> ..@ polygons :List of 177 #> ..@ plotOrder : int [1:177] 136 7 28 31 169 23 9 74 5 84 ... #> ..@ bbox : num [1:2, 1:2] -16656124 -8451673 16656124 8375779 #> .. ..- attr(*, "dimnames")=List of 2 #> ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot # Merge df data into World World <- sp::merge(World, df, by = "name") tm_shape(World) + tm_polygons("1950", palette = "Blues", title = "Income class", contrast = 0.7, border.col = "gray30", id = "name") + tm_text("iso_a3", size = "AREA", col = "gray30", root = 3) + tm_style_classic()
创建于2018-03-21由 代表包 (v0.2.0)。
使用 append_data 功能来自 tmaptools ...
append_data
tmaptools
library(tmap) library(tmaptools) data(World) df <- read.csv(header = T, text = " Country,Population United States,1000 Germany,3000 Brazil,5000 France,6000 ") World <- append_data(World, df, key.shp = "name", key.data = "Country", ignore.na = T) tm_shape(World) + tm_polygons("Population", title = "Pop Class", palette = "Blues", contrast = 0.7, border.col = "gray30", id = "name") + tm_text("iso_a3", size = "AREA", col = "gray30", root = 3)