世界地图:
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()