扩展ggmap输出并摆脱灰色

时间:2017-09-07 16:03:19

标签: r ggmap

任何人都可以协助扩展我的ggmap输出吗?

我使用this site作为模型来制作我自己的地图

虚拟数据:

df <- data.frame(Current_zip_code=c(27103, 27540, 32669, 32765, 39180, 39553, 47403, 70118, 70119, 70364, 74114, 74133, 83703, 90045, 90263, 90278, 96706, 96707, 99506), total=c(1, 5, 7, 12, 15, 6, 17, 31, 12, 13, 6, 4, 4, 25, 41, 65, 33, 25, 22), latitude=c(36.066545, 35.643545, 29.640613, 28.656375, 32.292761, 30.400599, 39.121719, 29.952305, 29.974504, 29.626988, 36.126894, 36.04309, 43.668396, 33.960041, 34.035087, 33.871214, 21.338055, 21.345535, 61.224384), longitude=c(-80.30733, -78.83486, -82.59446, -81.21026, -90.87184, -88.65092, -86.57409, -90.12347, -90.08747, -90.72076, -95.94657, -95.88417, -116.25707, -118.3949, -118.70752, -118.37177, -158.02499, -158.08587, -149.77461))

library(fiftystater)
data("usa")
usa_center = as.numeric(geocode("United States"))
USAMap = ggmap(get_googlemap(center=usa_center, scale=2, zoom=4), extent="normal")

map_ex <- USAMap +
   geom_point(aes(x=longitude, y=latitude), data=df, col="orange", alpha=0.4, size=df$total*.2) + 
   scale_size_continuous(range=range(df$total))

enter image description here

输出地图偏离中心。有没有办法扩大它,以包括我们在阿拉斯加和夏威夷的朋友?我不喜欢灰色背景和轴标题。任何方式来扩大和摆脱灰色和轴?

我尝试添加此

scale_x_continuous( limits = c( -95.5 , -95.3 ) , expand = c( 0 , 0 ) )

但我收到了错误

1 个答案:

答案 0 :(得分:2)

df中的某些点位于USAMap区域之外。 可以将灰色区域放在map_ex中,将其x轴和y轴的范围设置为USAMap轴范围的相同值。

df <- data.frame(Current_zip_code=c(27103, 27540, 32669, 32765, 39180, 39553, 
47403, 70118, 70119, 70364, 74114, 74133, 83703, 90045, 90263, 90278, 96706, 
96707, 99506), total=c(1, 5, 7, 12, 15, 6, 17, 31, 12, 13, 6, 4, 4, 25, 41, 
65, 33, 25, 22), latitude=c(36.066545, 35.643545, 29.640613, 28.656375, 32.292761, 
30.400599, 39.121719, 29.952305, 29.974504, 29.626988, 36.126894, 36.04309, 
43.668396, 33.960041, 34.035087, 33.871214, 21.338055, 21.345535, 61.224384), 
longitude=c(-80.30733, -78.83486, -82.59446, -81.21026, -90.87184, -88.65092, 
-86.57409, -90.12347, -90.08747, -90.72076, -95.94657, -95.88417, -116.25707, 
-118.3949, -118.70752, -118.37177, -158.02499, -158.08587, -149.77461))

library(ggmap)   
usa_center <- as.numeric(geocode("United States"))
usa_center_map <- get_googlemap(center=usa_center, scale=2, zoom=4)
USAMap <- ggmap(usa_center_map, extent="normal")

# Get axes range of USAMap
axis_range <- attr(usa_center_map, "bb")

# Set axes range using the "limits" option of 
# scale_x_continuous and scale_y_continuous
map_ex <- USAMap +
   geom_point(aes(x=longitude, y=latitude), data=df, 
                  col="orange", alpha=0.4, size=df$total*.2) + 
   scale_size_continuous(range=range(df$total)) +
   scale_x_continuous(limits=c(axis_range$ll.lon,axis_range$ur.lon)) +
   scale_y_continuous(limits=c(axis_range$ll.lat,axis_range$ur.lat))
map_ex

enter image description here

否则,您可以下载包含df中所有点的较大地图。

us_map <- get_googlemap(center=c(-110,45), scale=2, zoom=3, size = c(640, 500))
map_ex <- ggmap(us_map) +
   geom_point(aes(x=longitude, y=latitude), data=df, 
                  col="orange", alpha=0.4, size=df$total*.2) + 
   scale_size_continuous(range=range(df$total)) 
map_ex

enter image description here