Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put the name of the district on the plot

Tags:

dictionary

plot

r

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!

like image 542
Dima Sukhorukov Avatar asked Feb 06 '15 15:02

Dima Sukhorukov


2 Answers

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

like image 128
Math Avatar answered Oct 24 '22 04:10

Math


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))
like image 3
koekenbakker Avatar answered Oct 24 '22 04:10

koekenbakker