Search code examples
rdictionaryggplot2geom

How show some points on a map in R


I used this code in R in order to show a map.

library(maps)
library(raster)
library(rgeos)
library(ggplot2)
library(dplyr)

# Iran map
iran <- getData("GADM", country = "Iran", level = 1)
mymap <- fortify(iran)
mymap$id <- as.integer(mymap$id)

dat <- data.frame(id = rownames(iran@data),
                  state = iran@data$NAME_1,
                  pr = c(530,-42,1673,75,206,544,1490,118,75,
                         40,105,191,111,810, 609,425,418,550, 40, 425, -54,-50,
                         16, 18, 133,425, -30, 241,63, 191,100)) %>% 
  mutate(color_province = case_when(pr <= 50 ~ 'green',
                                    pr > 150 ~ 'red',
                                    TRUE ~ 'yellow'))
dat$id <- as.integer(dat$id)

mydf <- inner_join(mymap, dat, by = "id")
centers <- data.frame(gCentroid(iran, byid = TRUE))
centers$state <- dat$state

ggplot() +
  geom_map(data = mydf,
           map = mymap,
           aes(map_id = id, group = group,
               x = long, y = lat,
               fill = as.factor(color_province))) +
  coord_map() +
  labs(x = "", y = "") +
  scale_fill_manual(values = c("green", "red", "yellow"),
                    name = "areas")

Now I want to add some points (cities) with different sizes which show the population, but I couldn't add points.

# Cities
Khorasan <- c(60,35)
Kerman <- c(54,30)
Sistan <- c(61,27)

Cities <- rbind(Khorasan, Kerman, Sistan) %>% 
  as.data.frame()
colnames(Cities) <- c("ras","bal")

# Show the cities on the map
points(x=Cities$ras, y=Cities$bal, col="slateblue", cex=3, pch=20)

Can you help me, how can I add some points to the map and the size of these points shows their pupolation?


Solution

  • I would propose a simple approach with geom_sf to deal with coordinate data. For more functionality see package sf. Country data from rnaturalearth.

    library(rnaturalearth)
    library(ggplot2)
    
    part <- ne_countries(country="Iran", returnclass="sf")
    
    Vahdat_Park <- c(51.484655, 35.653493)
    Khorasan <- c(60,35)
    Kerman <- c(54,30)
    Sistan <- c(61,27)
    
    sites <- data.frame(Vahdat_Park, Khorasan, Kerman, Sistan)
    sites <- cbind( longitude=stack(sites[1,]), latitude=stack(sites[2,]))
    sites
      longitude.values longitude.ind latitude.values latitude.ind
    1         51.48465   Vahdat_Park        35.65349  Vahdat_Park
    2         60.00000      Khorasan        35.00000     Khorasan
    3         54.00000        Kerman        30.00000       Kerman
    4         61.00000        Sistan        27.00000       Sistan
    
    ggplot(part) + 
      geom_sf() +
      geom_point(data=sites, 
        aes(x = longitude.values, y = latitude.values, col=latitude.ind))
    
    # adding a connection from Vahdat_Park to Khorasan
    line <- data.frame(x=c(51.48465, 60.00000 ), y=c(35.65349, 35.00000))
    
    ggplot(part) + 
      geom_sf() +
      geom_point(data=sites, 
        aes(x = longitude.values, y = latitude.values, col=latitude.ind)) +
      geom_line(data=line, aes(x=x, y=y ) )
    

    map and sites