2017-07-18 118 views
1

嗨可視化愛好者的調整大小,創建地圖與各國

我想創建一個彩色地圖的情節,像這樣的:like this one (來源:https://github.com/hrbrmstr/albersusa

我想這個地圖是有偏見的,以便各州的地區與我提供的價值成正比(特別是,我使用GPD值)。 我的意思是說,我想讓一些州看起來更大一些,一些更小,他們在現實中,但儘可能提醒真正的美國地圖。 沒有問題的狀態移動或形狀破壞。

任何想法?任何現成解決方案 目前我使用R和albersusa包,因爲它是我熟悉的東西。打開改變! 我當前的情節代碼:

  gmap<- 
      ggplot() + 
      geom_map(data = [email protected], map = cmap, 
        aes(fill =atan(y/x),alpha=x+y, map_id = name), 
        color = "gray50") + 
      geom_map(data = smap, map = smap, 
        aes(x = long, y = lat, map_id = id), 
        color = "black", size = .5, fill = NA) + 
      theme_map(base_size = 12) + 
      theme(plot.title=element_text(size = 16, face="bold",margin=margin(b=10))) + 
      theme(plot.subtitle=element_text(size = 14, margin=margin(b=-20))) + 
      theme(plot.caption=element_text(size = 9, margin=margin(t=-15),hjust=0)) + 
scale_fill_viridis()+guides(alpha=F,fill=F) 
+0

你可以嘗試製作cartogram?看[這裏](https://stackoverflow.com/questions/9319597/cartogram-choropleth-map-in-r)。 – Axeman

回答

1

這裏是一個非常醜陋的第一次嘗試,讓你開始,使用輪廓從maps包,並從dplyr一些數據操作。

library(maps) 
library(dplyr) 
library(ggplot2) 

# Generate the base outlines 
mapbase <- map_data("state.vbm")  

# Load the centroids 
data(state.vbm.center) 

# Coerce the list to a dataframe, then add in state names 
# Then generate some random value (or your variable of interest, like population) 
# Then rescale that value to the range 0.25 to 0.95 

df <- state.vbm.center %>% as.data.frame() %>% 
    mutate(region = unique(mapbase$region), 
     somevalue = rnorm(50), 
     scaling = scales::rescale(somevalue, to = c(0.25, 0.95))) 
df 

# Join your centers and data to the full state outlines 
df2 <- df %>% 
    full_join(mapbase) 
df2 

# Within each state, scale the long and lat points to be closer 
# to the centroid by the scaling factor 

df3 <- df2 %>% 
    group_by(region) %>% 
    mutate(longscale = scaling*(long - x) + x, 
     latscale = scaling*(lat - y) + y) 
df3 

# Plot both the outlines for reference and the rescaled polygons 

    ggplot(df3, aes(long, lat, group = region, fill = somevalue)) + 
    geom_path() + 
    geom_polygon(aes(longscale, latscale)) + 
    coord_fixed() + 
    theme_void() + 
    scale_fill_viridis() 

enter image description here

這些輪廓是不是最好的,而且重心位置,他們向收縮引起的多邊形有時重疊原始狀態的輪廓。但這是一個開始;您可以爲美國各州和各種質心算法找到更好的形狀。

+0

哇哇!非常感謝! –