How to plot map with place names - ggplot - R

Asked

Viewed 321 times

5

I downloaded the data and plotted a map with the demographic density of the cities of the Metropolitan Region of São Paulo. But it is difficult to identify the cities.

rmsp <- readOGR("rmsp", "MunRM07", stringsAsFactors = F, encoding = "latin1")

rmsp_data <- rmsp@data
rmsp_poligonos <- rmsp@polygons
rmsp_projecao <- rmsp@proj4string

url_munic_15 <- "https://raw.githubusercontent.com/leobarone/FLS6397/master/data/planejamento_munic_2015.csv"
munic_15 <- read.table(url_munic_15, header = T, sep = ";") %>%
   rename(COD_IBGE = A1, ano_pd = A18) %>%
   select(COD_IBGE, ano_pd) %>%
   mutate(ano_pd = as.numeric(as.character(ano_pd)))

rmsp@data <- rmsp@data %>% 
   mutate(ID = as.numeric(ID), COD_IBGE = as.numeric(COD_IBGE),
          DENS_DEMO = as.numeric(DENS_DEMO)) %>%
   left_join(munic_15, by = "COD_IBGE")

 rmsp_df <- fortify(rmsp)

rmsp_df$id <- as.numeric(rmsp_df$id)
   rmsp@data$id <- 0:(nrow(rmsp@data)-1)
   rmsp_df <- left_join(rmsp_df, rmsp@data, by = "id")

ggplot(data = rmsp_df, 
       aes(x = long, y = lat, group = group, fill = DENS_DEMO)) + 
  geom_polygon() +
  coord_map()

How to identify the places to get a sense of the positions on the map?

2 answers

3


If we put the function label inside aes() before we process the data, for each row in df it will plot a name. We will have thousands of repetitions overlapping.

The solution I found was to create an object grouping the names and an average position of the latitudes and longitudes to locate where the name will be plotted. I also found it necessary to filter cities by size, because smaller and less known cities overlapped larger and better known cities, which makes localization difficult.

nome_cidade <- rmsp_df %>% # Cria o objeto 
   filter(as.numeric(AREA_KM2) > 100) %>% # Filtra cidades com areas maiores de 100km2
   group_by(NOME) %>% # Agrupa para nomes únicos evitando repetição e sobreposição 
   summarise(long = mean(long), lat = mean(lat)) # Tira a média das coordenadas

Now we plot the map by inserting the line geom_text with the parameters based on the object created above. It is the one who will put the names.

ggplot(data = rmsp_df, 
       aes(x = long, y = lat)) + 
   geom_polygon(aes(group = group, fill = DENS_DEMO)) +
   geom_text(aes(label = NOME), check_overlap = T, data = nome_cidade, color = "white", size = 3, hjust = 0.5) +
   coord_map()

Important to note the check_overlap = T if you don’t want names overlapping on the map.

1

One option is to use the centroids to plot the names. Since you didn’t post your shapefile, I’m using one that I already have for example and simulating some random data:

shape <- rgdal::readOGR('~/Shapefiles/', 'BRregioes')

> shape@data
  CD_GEOREG    NM_REGIAO
0         1        Norte
1         5 Centro-Oeste
2         2     Nordeste
3         3      Sudeste
4         4          Sul

dados <- data.frame(
  Regiao = shape@data$NM_REGIAO,
  variavel = rnorm(5, 10) )

Using geom_map no need to merge the data of the regions with the shapefile, just a column of identification with the same names. For labels, calculate the centroids and use this information along with the data.frame with the data:

centroides <- as.data.frame(rgeos::gCentroid(shape, byid = TRUE))
centroides$Regiao <- shape@data$NM_REGIAO

shape.f <- fortify(shape, region = 'NM_REGIAO')

ggplot(merge(dados, centroides)) +
  geom_map(map = shape.f, aes(map_id = Regiao, fill = variavel)) +
  geom_text(aes(x, y, label = Regiao), color = 'white') +
  expand_limits(x = shape.f$long, y = shape.f$lat) +
  coord_map() +
  theme_void()

inserir a descrição da imagem aqui Can use geom_label in place of geom_text if you want more emphasis on the names.

Browser other questions tagged

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