如何在ggplot中更改地图的大小

时间:2016-11-24 21:02:52

标签: r ggplot2

    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"))

问题是当我用机场打印我的地图时,我看不到美国大陆,只看到我的机场,美国的地图那么小,我不知道如何在我的中间“打印”它领域,并使更大。 这是否意味着我有太多的数据? 谢谢!

3 个答案:

答案 0 :(得分:1)

我不太确定您想要什么,但是当您添加<% @monsters.each do |monster| %> <tr> <td><%= number_field_tag "monster[#{monster.id}][quantity]", 0, within: 0...10 %></td> </tr> <% end %> scale_x_continuous时,您可以将映射区域缩小并“放大”到美国。

如果您对映射感兴趣,建议您查看tmap(https://cran.r-project.org/web/packages/tmap/tmap.pdf)。它也可以产生点图。

scale_y_continuous

enter image description here

答案 1 :(得分:1)

更新:以下是如何添加状态&#39;地图的缩写。数据来自评论中的链接。

# 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

答案 2 :(得分: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