ggplot2:畫世界地圖和中國地圖 合並數據 增添信息 標記


世界地圖:

library(maps)
data("world.cities")
bigcities <- subset(world.cities, pop > 5000000)
qplot(long, lat, data = bigcities,colour=country.etc,size=pop)+ borders("world", size= 0.5)

中國地圖:

library(mapdata)

ch_cities <- subset(world.cities, country.etc=="China")
ggplot(ch_cities, aes(long, lat)) + geom_point(colour= alpha("red",0.5))+ borders("china")#

合並數據到地圖數據:

  

states <- map_data("state")##將地圖數據轉為數據框
arrests <- USArrests ##新數據(要與地圖數據中的某一列匹配)
names(arrests) <- tolower(names(arrests)) ##將列名翻譯為小寫,因為state數據的region為小寫
arrests$region <- tolower(rownames(USArrests)) ##增添新列 region

choro <- merge(states, arrests, by = "region")

# Reorder the rows because order matters when drawing polygons
# and merge destroys the original ordering
choro <- choro[order(choro$order), ]
qplot(long, lat, data = choro, group = group, 
  fill = Assault, geom = "polygon") ##多邊形
qplot(long, lat, data = choro, group = group, 
  fill = Assault / murder, geom = "polygon")


ia <- map_data("county", "iowa")
library(plyr)
mid_range <- function(x) mean(range(x, na.rm = TRUE))
centres <- ddply(ia, .(subregion), 
  colwise(mid_range, .(lat, long)))  ##計算區的中心位置
ggplot(ia, aes(long, lat)) + 
  geom_polygon(aes(group = group), 
    fill = NA, colour = "grey60") +
  geom_text(aes(label = subregion), data = centres,  ##對地圖標注
    size = 2, angle = 45)


出現問題時:
dev.off()

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM