How to insert a legend on a map in R?

Asked

Viewed 832 times

1

I did a cluster analysis k-Means fictitious. I want to create a caption (cluster1, cluster2 and cluster3), which is represented by the variable "kmeans" (formed clusters). I do not know if it would be possible to insert a scale. If possible, how to do?

library(sp)
library(rgdal)
library(RColorBrewer)

map<-readOGR("C:\\Users\\EU\\Desktop\\file","UFEBRASIL")

head(map@data)
ID CD_GEOCODU NM_ESTADO NM_REGIAO
0  1         11 RONDÔNIA     NORTE
1  2         12      ACRE     NORTE
2  3         13  AMAZONAS     NORTE
3  4         14   RORAIMA     NORTE
4  5         15     PARÃ     NORTE
5  6         16    AMAPÃ     NORTE`

map@data$newvar1<-runif(27,20,100)
map@data$newvar2<-runif(27,20,100)
map@data$newvar3<-runif(27,20,100)
map@data$newvar4<-runif(27,20,100)
map@data$newvar5<-runif(27,20,100)
map@data$newvar6<-runif(27,20,100)

head(map@data)
ID CD_GEOCODU NM_ESTADO NM_REGIAO  newvar1  newvar2  newvar3  newvar4
0  1         11 RONDÔNIA     NORTE 94.90707 21.74030 37.29604 59.26561
1  2         12      ACRE     NORTE 65.35450 66.58382 40.35771 75.76805
2  3         13  AMAZONAS     NORTE 43.70452 26.27472 47.90803 58.78851
3  4         14   RORAIMA     NORTE 25.78709 54.68699 40.98279 35.85327
4  5         15     PARÃ     NORTE 80.20983 63.18456 39.09327 82.31181
5  6         16    AMAPÃ     NORTE 51.39521 51.89274 81.59527 45.32389
newvar5  newvar6 kmeans
0 87.44586 97.69417      1
1 50.92863 87.48382      1
2 64.41917 75.71042      1
3 21.01914 86.78704      1
4 68.11799 87.51693      1
5 75.82186 59.10289      2


palette(brewer.pal(5,"Blues")
palette()
plot(map,col=map@data$kmeans)

Mapa resultate

download the shapefile: http://www.usp.br/nereus/? data=brazil (Federative units)

1 answer

3


There are several ways to do this. Using ggplot2:

library(rgdal)
library(ggplot2)
library(RColorBrewer)

shapeUFs <- readOGR('.', 'UFEBRASIL')

# Simulando o resultado do agrupamento por k-means:
mediask <- data.frame(
  CD_GEOCODU = shapeUFs@data$CD_GEOCODU,
  cluster = sample(1:4, nrow(shapeUFs@data), replace = TRUE)
)

# Convertendo o shape para data.frame (necessário para usar geom_map):
mapaUFs <- fortify(shapeUFs, region = 'CD_GEOCODU')

ggplot(mediask) +
  geom_map(
    map = mapaUFs,
    color = 'gray20',
    aes(map_id = CD_GEOCODU,
        fill = as.factor(cluster)) 
  ) +
  expand_limits(  # porque o data.frame com os clusters não possui coordenadas
    x = mapaUFs$long,
    y = mapaUFs$lat
  ) +
  scale_fill_brewer(
    'Cluster',
    palette = 'Accent'
  ) +
  coord_map() +  # define uma projeção, para que o mapa mantenha as proporções
  theme_void()   # para não exibir grade, eixos, etc

inserir a descrição da imagem aqui

The main advantage of using ggplot with geom_map is that you do not need to merge your data with the spatial object, just have an identification column to tell you where each value will be plotted.

I used as.factor(cluster) so that the scale is generated as categories. So I also used a qualitative palette; but nothing prevents you from using 'Blues' or any other.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.