下面的一段代码显示了我的问题
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)