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