0

下面的一段代码显示了我的问题

library(rgdal)
library(RColorBrewer)
library(tmap)
library(maptools)
data(Europe)
first <- c("a", "a", "a", "d", "d", "d", "c", "c", "c", "c")
second <- c("c", "c", "c", "d", "d", "d", "d", "e", "e", "e")
kod <- c("POL", "DEU", "ESP", "FRA", "CZE", "SVK", "GBR", "ITA", "UKR", "RUS")
nazwy <- data.frame(first, second, kod)
Europe_nazwy <- merge(Europe, nazwy, by.x="iso_a3",by.y="kod")
tm1 <- tm_shape(Europe_nazwy) +  tm_polygons("first", palette="Set1") +
tm_text("first", size="AREA", root=5) +  tm_layout(frame=F, legend.outside = TRUE)
tm2 <- tm_shape(Europe_nazwy) +  tm_polygons("second", palette="Set1") + 
tm_text("second", size="AREA", root=5) +  tm_layout(frame=F, legend.outside = TRUE)
tmap_arrange(tm1, tm2, asp = NA)

它产生两张地图。我想要的是两个地图上的相同类别具有相同的颜色(例如 c 在两个地图上都是蓝色的,而 d 在两个地图上都是红色的,目前还不是)。有什么建议么?我已经尝试过 ggplot2 的解决方案(下面的代码),但它没有用。我猜 scale_colour_manual 在 tmaps 中不起作用?

library(ggplot2)
kolory <- c("a", "b", "c", "d", "e")
myColors <- brewer.pal(5,"Set3")
names(myColors) <- levels(kolory)
colScale <- scale_colour_manual(name = kolory,values = myColors)

tm1 <- tm_shape(Europe_nazwy) +  tm_polygons("first") + 
tm_text("first", size="AREA", root=5) + 
tm_layout(frame=F, legend.outside = TRUE) + colScale

tm2 <- tm_shape(Europe_nazwy) +  tm_polygons("second") + 
tm_text("second", size="AREA", root=5) + 
tm_layout(frame=F, legend.outside = TRUE) + colScale
tmap_arrange(tm1, tm2, asp = NA)
4

1 回答 1

2

好的,所以等待您的帮助,我想我自己找到了一种可能的解决方案。我所要做的就是为每个地图定义调色板(palete1& palete2),记住值是按字母顺序分配的颜色。

library(rgdal)
library(RColorBrewer)
library(tmap)
library(maptools)
data(Europe)

first <- c("a", "a", "a", "d", "d", "d", "c", "c", "c", "c")
paleta1 <- c("red", "green", "blue")
second <- c("c", "c", "c", "d", "d", "d", "d", "e", "e", "e")
paleta2 <- c( "green", "blue", "yellow")
kod <- c("POL", "DEU", "ESP", "FRA", "CZE", "SVK", "GBR", "ITA", "UKR", "RUS")
nazwy <- data.frame(first, second, kod)

Europe_nazwy <- merge(Europe, nazwy, by.x="iso_a3",by.y="kod")

tm1 <- tm_shape(Europe_nazwy) +  tm_polygons("first", palette=paleta1) +
tm_text("first", size="AREA", root=5) +  tm_layout(frame=F, legend.outside = TRUE)

tm2 <- tm_shape(Europe_nazwy) +  tm_polygons("second", palette=paleta2) +
 tm_text("second", size="AREA", root=5) +  tm_layout(frame=F, legend.outside = TRUE)
tmap_arrange(tm1, tm2, asp = NA)
于 2018-03-11T14:10:02.483 回答