4

How to control the dot colors in tmap? I have the following code:

png(filename = "test.png", 
    width = 210, height = 297, units = "mm", pointsize = 1,
    bg = "white", res = 1200)
for (j in 2012:2016){
t <- tm_shape(n2000) +
  tm_borders("black") +
  tm_shape(dk) + 
  tm_fill("grey30") +
  tm_borders("grey70") +
  tm_shape(out3[out3$Country==i & out3$Year == j,]) +
  tm_dots("LE_GEAR",            
          size = 1, title=paste0("Type , ", i, ", ", j)) +
   tm_legend(text.size=10,
            title.size=12,            
            position = c("left","top"), 
            bg.color = "white", 
            bg.alpha=.2, 
            frame="gray50", 
            height=.6, 
            hist.width=20,
            hist.height=20, 
            hist.bg.color="gray60", 
            hist.bg.alpha=.5) +
  tm_shape(Labels) +
  tm_text("Name", size = 4) 

assign(paste0("t", j), t)
}

tm <- tmap_arrange(t2012, t2013, t2014, t2015, t2016)#, asp = NA)

graphics.off()

Which gives me the map below. The problem is that the colors for for example GNS is different from 2012 to the other years. How can i make sure the colors are the same?

enter image description here

2

The trick is to create a custom color palette. First, a reproducible example:

library(sp)
library(tmap)

data(meuse, meuse.area)       

coordinates(meuse) <- ~x + y 

meuse.area <- SpatialPolygons(list(Polygons(list(Polygon(meuse.area)),'1')))

proj4string(meuse)<- CRS("+init=epsg:28992")
proj4string(meuse.area)<- CRS("+init=epsg:28992")

set.seed(123)

# Package example
require(tmaptools)
data(World)
x <- sample_dots(World, vars="gdp_md_est", convert2density = TRUE, w = 100000)
x <- SpatialPointsDataFrame(x, data.frame(type=factor(sample(1:5,size=length(x),replace = T))))

tm_shape(x) + 
  tm_dots()

enter image description here

Identify unique features prior to create a color palette. I used rainbow color function, but you can choose your own colors in hex format.

# To set color, first identify unique features
types <- unique(x$type)
types <- sort(types)

# asssigg one color per feature (I used rainbow for hex colors)
color_type <- rainbow(length(types))

# Use the above color palette
tm_shape(x) + 
  tm_dots('type',palette=color_type)

enter image description here

Finally, use this information to set the color palette en each iteration.

for(i in 1:5){
  temp <- x[x@data$type==types[i],]
  temp$type  <- droplevels(temp$type)
  t <- tm_shape(temp) + 
    tm_dots('type',palette=color_type[i])
  assign(paste0('t',i),t)
}

tmap_arrange(t1,t2,t3,t4,t5)

enter image description here

So, in your case, you need to identify which feature is present by year and select the right color schema. Something like:

classes <- unique(out3$LE_GEAR)
classes <- sort(classes)
classcolor <- rainbow(length(classes))

for (j in 2012:2016){
  temp <- out3[out3$Country==i & out3$Year == j,]
  temp$LE_GEAR <- droplevels(temp$LE_GEAR)
  actual_classes <- unique(temp$LE_GEAR)
  actual_classes <- sort(actual_classes)
  t <- tm_shape(n2000) +
    tm_borders("black") +
    tm_shape(dk) + 
    tm_fill("grey30") +
    tm_borders("grey70") +
    tm_shape(temp) +
    tm_dots("LE_GEAR",            
            size = 1, title=paste0("Type , ", i, ", ", j),
            palette=classcolor[match(actual_classes,classes)]) +
    tm_legend(text.size=10,
              title.size=12,            
              position = c("left","top"), 
              bg.color = "white", 
              bg.alpha=.2, 
              frame="gray50", 
              height=.6, 
              hist.width=20,
              hist.height=20, 
              hist.bg.color="gray60", 
              hist.bg.alpha=.5) +
    tm_shape(Labels) +
    tm_text("Name", size = 4) 

  assign(paste0("t", j), t)
}

tm <- tmap_arrange(t2012, t2013, t2014, t2015, t2016)

Hope this helps you... Without sample data, I can't help you better. I suppose j is a nested loop and i is the parent (not present in the code you give us)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.