2016-11-24 128 views
1
airportsUS <- subset(airports, iso_country == "US") #select only US airports 

    map <- borders("usa", colour="black", fill="white", size = .3) #map USA continent 

    airportsmap <- ggplot(airportsUS) + map 

print(airportsmap + geom_point(aes(x=airportsUS$longitude_deg, 
    y=airportsUS$latitude_deg), 
    shape=3, size = .1, color = "red")+ 
    theme(legend.position = "top")+ 
    ggtitle("Airports")) 

問題是,當我打印我的地圖機場,我看不到美國本土,只有我的機場,和美國的地圖那麼小,我不知道如何在我的領域中「打印」它並做出更大的選擇。 這是否意味着我有太多數據? 謝謝!如何更改地圖的大小ggplot

回答

1

我不太清楚你想要什麼,但是當你添加scale_x_continuousscale_y_continuous有限制時,可以使映射區域變小並「放大」到美國。

如果您對繪圖感興趣,建議您查看tmap(https://cran.r-project.org/web/packages/tmap/tmap.pdf)。它也可以生成點圖。

airports <- read.csv("http://bl.ocks.org/mbostock/raw/7608400/airports.csv", stringsAsFactors=FALSE) 
airportsUS <- airports 
map <- borders("usa", colour="black", fill="white", size = .3) #map USA continent 

airportsmap <- ggplot(airportsUS) + map 

print(airportsmap + geom_point(aes(x=airportsUS$longitude, 
            y=airportsUS$latitude), 
           shape=3, size = .1, color = "red")+ 
     theme(legend.position = "top")+ 
     scale_x_continuous(limits = c(-125, -67))+ 
     scale_y_continuous(limits = c(25, 50))+ 
     ggtitle("Airports")) 

enter image description here

+0

謝謝!完美的作品!正是我想要的。我在地圖和可視化中很有趣,這就是爲什麼感謝你的鏈接! – Pon4a

+0

不用客氣 - 也可以看看:https://cran.r-project.org/web/packages/tmap/vignettes/tmap-nutshell.html這是一個很好的概述,tmap真的很強大! – Mario

1

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

+0

謝謝,看起來很酷! 但我在你的地圖中看到了我真正需要的東西。國界。 現在我正在閱讀有關地圖的知識,也許你知道如何在地圖上包含州名?像NY,CA等 – Pon4a

+0

我被查看了您的帳戶,抱歉)您是DATA科學家!這真棒,因爲我現在正在德國學習=)這應該是我的項目在12月)) – Pon4a

+0

嗨Nadya :)我是德國人,但在美國長大。 – katharina

0

以防萬一。我與馬里奧和卡塔琳娜碼一起工作,並將其放在地圖國家的邊界​​上。而現在的代碼如下所示:

airports <- read.csv("airports.csv",header=TRUE,as.is=TRUE) 


library(ggplot2) 
library(ggmap) 
library(maps) 
airportsUS <- subset(airports, iso_country == "US") 
all_states <- map_data("state") 
statecenter<-data.frame(long=tapply(all_states$long,all_states$region,mean),lang=tapply(all_states$lat,all_states$region,mean)) 

g<- ggplot() 
statemap <- g + geom_polygon(data=all_states, 
          aes(x=long, y=lat, group = group), 
          colour="black", fill="white") 
statemap 


airports_usa_map <- print(statemap + geom_jitter(aes(x=airportsUS$longitude, y=airportsUS$latitude, 
           color=airportsUS$type), 
          shape=3, size = .1)+ 
     geom_text(aes(x=statecenter$long,y=statecenter$lang,label=rownames(statecenter)),size=3,vjust=-1)+ 
     theme(legend.position = "top")+ 
     scale_x_continuous(limits = c(-125, -67))+ 
     scale_y_continuous(limits = c(25, 50))+ 
     ggtitle("USA Airports")) 
#ggsave("airports_usa_map.png") 

,我們會盡快: all US airports by types, and states name

+0

您可能會考慮採用不同的機場類型:http://docs.ggplot2.org/current/facet_grid.html - 這將爲您提供每種機場的地圖 – Mario