2016-11-24 132 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