Home > Enterprise >  How show some points on a map in R
How show some points on a map in R

Time:12-26

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?

CodePudding user response:

I would propose a simple approach with geom_sf to deal with coordinate data. For more functionality see map and sites

  • Related