0

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)
Glomek
  • 71
  • 7
  • 1
    I get an error saying that `Europe_nazwy` is not a spatial object. Maybe you have another package loaded that provides a better merge method? ( and you _should_ also provide code that loads the `tmap` package) – IRTFM Mar 10 '18 at 20:28
  • @42- I've suplemented the code with all packages attached to it to work. `Europe` is a dataset included in `tmap` package and it is a SpatialPolygonsDataFrame so after merging with `nazwy` it still stays SpatialPolygonsDataFrame isn't it? – Glomek Mar 10 '18 at 21:01
  • That's true only if there is a `merge`-method in one of the other packages that are loaded. The sp-package has such a function and perhaps either the rgdal or maptools packages do as well. When I did it initially, the class of the merged object was just "data.frame". – IRTFM Mar 11 '18 at 01:56
  • `tmap` installs `sp` – Glomek Mar 11 '18 at 05:03
  • You probably mean "loads" rather than "installs". It wasn't until I explicitly loaded `sp` that I got the behavior you were expecting. Perhaps sp loads a package with it rather than having the method itself. – IRTFM Mar 11 '18 at 16:21

1 Answers1

2

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)
Glomek
  • 71
  • 7