UPDATE:這裏是你如何添加狀態的縮寫到地圖中。數據來自評論中的鏈接。
# http://openflights.org/data.html
# https://statetable.com/
# https://inkplant.com/code/state-latitudes-longitudes
library(ggplot2)
library(maps)
cols = c("airport_id", "name", "city", "country", "iata_faa", "icao",
"latitude", "longitude", "altitude", "timezone", "dst",
"tz_timezone")
airports <- read.csv("airports.dat.txt", header=F, col.names=cols)
states_data <- read.csv('states.csv')
states_geo <- read.csv('states_geo.csv')
states <- tolower(states_data$name)
abbrs <- tolower(states_data$abbreviation)
states_dict <- list()
for (i in seq_along(states)) {
state <- states[[i]]
abbr <- abbrs[[i]]
states_dict[[state]] <- abbr
}
lookupAbbr <- function(x) {
ab <- states_dict[[tolower(x)]]
if (is.null(ab)) {
return("")
} else {
return(ab)
}
}
states_geo$State <- as.character(states_geo$State)
states_geo$abbr <- sapply(states_geo$State, function(x) lookupAbbr(x))
states_geo$abbr <- toupper(states_geo$abbr)
states_geo <- subset(states_geo, !abbr %in% c("AK", "HI"))
airportsUS <- subset(airports, country=="United States")
airportsUS <- subset(airportsUS, latitude > 23 & latitude < 48)
airportsUS <- subset(airportsUS, longitude < -30 & longitude > -130)
m <- ggplot() +
geom_polygon(data=map_data("state"), aes(x=long, y=lat, group=group),
colour="white", fill="gray")
m + geom_point(data=airportsUS, aes(x=longitude, y=latitude),
color="red") +
theme_bw() + coord_equal() +
theme(panel.background = element_blank(), panel.grid.minor = element_blank(),
panel.grid.major = element_blank(), axis.ticks = element_blank(),
axis.title.x = element_blank(), axis.title.y = element_blank(),
axis.text.x = element_blank(), axis.text.y = element_blank()) +
geom_text(data=states_geo, aes(label=abbr, x=Longitude, y=Latitude))
ggsave("map.png")
map image
謝謝!完美的作品!正是我想要的。我在地圖和可視化中很有趣,這就是爲什麼感謝你的鏈接! – Pon4a
不用客氣 - 也可以看看:https://cran.r-project.org/web/packages/tmap/vignettes/tmap-nutshell.html這是一個很好的概述,tmap真的很強大! – Mario