2015-10-28 40 views
1

我使用下面的代碼創建一個高分辨率的地圖:便利出口高分辨率地圖明智的字體和大小傳奇

# Data sourcing ----------------------------------------------------------- 

# Download an read US state shapefiles 
tmp_shps <- tempfile(); tmp_dir <- tempdir() 
download.file("http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip", 
       tmp_shps) 
unzip(tmp_shps, exdir = tmp_dir) 

# Libs 
require(rgdal); require(ggplot2) 

# Read 
us_shps <- readOGR(dsn = tmp_dir, layer = "cb_2014_us_state_20m") 
# Prepare data set for ggplot2 
us_shps_frt <- fortify(us_shps, region = "NAME") 

# Fit map ----------------------------------------------------------------- 

# Lib 
require(ggmap) 

# Get box for the 
bbox <- make_bbox(lon = long, lat = lat, 
        data = us_shps_frt[grep("South", us_shps_frt$id),], 
        f = 0.5) 
map_backgr <- get_map(bbox, maptype = "roadmap", zoom = 5, 
         source = "google", scale = 2, messaging = TRUE) 
map_backgr <- ggmap(map_backgr, extent = "normal", maprange = FALSE) 

# Libs 
require(grid) 


# Generate and export ----------------------------------------------------- 

exp_map <- map_backgr + 
    geom_polygon(data = us_shps_frt[grep("South", us_shps_frt$id),], 
       aes(x = long, y = lat, group = group, 
        fill = id)) + 
    coord_equal() + 
    ggtitle("Odd Map with Background") + 
    theme_map() + 
    theme(legend.background = element_rect(colour = 'black'), 
     plot.title = element_text(face = 'bold', size = 7), 
     legend.position = c(0.8,0), 
     legend.text = element_text(size = 5), 
     legend.title = element_text(size = 5, face = 'bold'), 
     legend.background = element_rect(size = 0.1, colour = 'black', 
             linetype = 'solid'), 
     plot.margin = unit(rep(5,4),"mm")) 


## Export 
ggsave(filename = "exp_map.png", 
     plot = exp_map, width = 7, height = 7, units = 'cm', 
     scale = 2, dpi = 600) 

的代碼生成此地圖:

Ugly map

我想更改以下內容:

  1. 圍繞圖例的輪廓線 - 我試過通過theme設置來控制它,但傳奇大綱仍然很大。
  2. 刪除文字周圍的白邊,爲什麼儘管plot.margin = unit(rep(5,4),"mm")設置被添加到theme上,情節被充足的白色邊距所包圍?
+0

添加了關於顏色部分未對齊到我的答案的簡短評論 – Jaap

回答

2

你將不得不邊玩邊緣和情節的寬度。下面將爲你所需要的:

exp_map <- map_backgr + 
    geom_polygon(data = us_shps_frt[grep("South", us_shps_frt$id),], 
       aes(x = long, y = lat, group = group, fill = id), alpha = 0.7) + 
    scale_x_continuous(expand = c(0, 0)) + 
    scale_y_continuous(expand = c(0, 0)) + 
    ggtitle("Odd Map with Background") + 
    theme(legend.background = element_rect(colour = 'black'), 
     plot.title = element_text(face = 'bold', size = 12), 
     legend.position = c(0.2,0.2), 
     legend.text = element_text(size = 8), 
     legend.title = element_text(size = 10, face = 'bold'), 
     legend.background = element_rect(colour = 'black', linetype = 'solid'), 
     axis.ticks = element_blank(), axis.text = element_blank(), axis.title = element_blank(), 
     plot.margin = unit(c(0,-0.4,-1,-1.2),"lines")) 

ggsave(filename = "exp_map3.png", 
     plot = exp_map, width = 6.7, height = 7, units = 'cm', 
     scale = 2, dpi = 600) 

正如你所看到的,我也去掉了coord_equal()部分。這會導致彩色部件未對齊(請參閱this explanation)。