Sunday, April 14, 2013

RG#61: Plotting US or World Cities

require(maps)
map("world")
data(world.cities)
# cities with minimum 20,000 population
map.cities(world.cities, country = "", minpop = 20000, maxpop = Inf,
pch = ".", col = "red")





map("world", col = "gray70", fill = TRUE)
data(world.cities)

# now device colors from yvar data category
# brewing color for continious color filling



 library(RColorBrewer)
plotclr <- brewer.pal(7,"YlOrRd")#



# categorize in different class for yvar
world.cities$colrs <- as.numeric(cut(world.cities$pop, c(0, 250000, 500000, 750000, 1000000, 1250000, 1500000,Inf)))

# corresponding legend text
legdtxt <- c("<0.25M", "0.25-0.50M", "0.50-0.75M", "0.75-1M", "1-1.5M", ">1.5M")

 map.cities(world.cities, country = "", minpop = 200000, maxpop = Inf, pch = 19, col = plotclr[world.cities$colrs])
legend("bottomleft", legdtxt, horiz = FALSE, fill = plotclr)
 


# world map cities pch proportional to population and color 
map("world", col = "lightgreen", fill = TRUE)
 
data (world.cities)
 
library(RColorBrewer)
plotclr <- brewer.pal(7,"YlOrRd")#
# categorize in different class for yvar world.cities$colrs <- as.numeric(cut(world.cities$pop, c(0, 250000, 500000, 750000, 1000000, 1250000, 1500000,Inf)))
# corresponding legend text
legdtxt <- c("<0.25M", "0.25-0.50M", "0.50-0.75M", "0.75-1M", "1-1.5M", ">1.5M")
 


map.cities(world.cities, country = "", minpop = 100000, maxpop = Inf, pch = 19,
 col =  plotclr[world.cities$colrs], cex = world.cities$colrs *0.5)
 
legend("bottomleft", legdtxt, horiz = FALSE, fill = plotclr)




 # same map in US
 map('usa', fill = TRUE, col = "lightgreen")
 data(us.cities)
  library(RColorBrewer)
  plotclr <- brewer.pal(6,"YlOrBr")## categorize in different class for yvar
  us.cities$colrs <- as.numeric(cut(us.cities$pop, c(0, 50000, 500000, 750000, 1000000, Inf)))
   # corresponding legend text
   legdtxt <- c("<0.05M", "0.05-0.50M", "0.50-0.75M", "0.75-1M",  ">1M")
   map.cities(us.cities, country = "", minpop = 50000, maxpop = Inf, pch = 19,
    col =  plotclr[us.cities$colrs], cex = us.cities$colrs)
 legend("bottomleft", legdtxt, horiz = FALSE, fill = plotclr, cex = 0.6)



 
 
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.