5

I am trying to make a plot of the intensity map of Ukraine's regions and intensity depends on the 'value'. Here is my code:

library(sp)
con <- url("http://biogeo.ucdavis.edu/data/gadm2/R/UKR_adm1.RData")
print(load(con))
close(con)
name<-VARNAME_1
gadm$VARNAME_1
value<-c(1:27)
gadm$VARNAME_1<-as.factor(value)
col<- colorRampPalette(c('white', 'black'))(256) 
spplot(gadm, "VARNAME_1", main="Ukraine", scales = list(draw = TRUE), col.regions=col)

My question is: Is it possible to put on the plot the names of the regions (I have it as a character vector name in my code to the appropriate place on the map. Or maybe another suggestion to make a map more clear and understandable which region has corresponding value. Thank you!

Bonifacio2
  • 3,405
  • 6
  • 34
  • 54
Dima Sukhorukov
  • 129
  • 4
  • 13

2 Answers2

4

one possibility is to do it with colors :

colors=rainbow(length(gadm$NAME_1))
plot(gadm,col=colors)
legend("topleft",legend=gadm$NAME_1,fill=colors,cex=1.3,bty="n" )

enter image description here

or you add names with text :

colors=rainbow(length(gadm$NAME_1))
plot(gadm,col=colors)
text(coordinates(gadm), labels = gadm$NAME_1)

enter image description here

Math
  • 1,274
  • 3
  • 14
  • 32
  • You can change the colors if you want – Math Feb 06 '15 at 15:58
  • You can also get more informations about plot function here : http://stackoverflow.com/questions/20168230/how-to-add-color-to-map-by-region – Math Feb 06 '15 at 16:09
3

This solution uses plot instead of spplot so we can add labels with text. If you still want to use spplot, check

x11()
col = cm.colors(length(gadm$PID))
plot(gadm, , col=col[rev(gadm$VARNAME_1)])
text(coordinates(gadm), labels = gadm$NAME_1, cex=0.4)

Or if you still want to use spplot, be prepared to do a bit extra. Here's a modification of this answer

sp.label <- function(x, label) {
    list("sp.text", coordinates(x), label)
}

NAME.sp.label <- function(x) {
    sp.label(x, x$NAME_1)
}

draw.sp.label <- function(x) {
    do.call("list", NAME.sp.label(x))
}

spplot(gadm, 'VARNAME_1', sp.layout = draw.sp.label(gadm))
Community
  • 1
  • 1
koekenbakker
  • 3,524
  • 2
  • 21
  • 30