Reputation: 71
the piece of code below shows my problem
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)
it produces two maps. what I want is same categories on both map have same color (like c is blue on both maps and d is red on both maps which it is not at the moment). any suggestions? I've tried solution for ggplot2 (code below) but it didn't work. I guess scale_colour_manual does not work in 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)
Upvotes: 0
Views: 1902
Reputation: 71
ok, so waiting for your help i think i found one possible solution myself. all i had to do was to define color palete (palete1
& palete2
) for each map bearing in mind that values are assigned colors in alphabetical order.
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)
Upvotes: 2