2017-08-29 82 views
2

我對使用R相對比較陌生,我試圖使用數據創建美國各州的地圖來勾勒和顏色某些區域。我試圖展示一個黑色的州和縣。最重要的是,我想圍繞各個縣市制作濃厚的紅色邊框,並根據我擁有的一些數據填充一些縣。我如何使用ggplot2在美國的一些縣周圍創建邊界?

基本上我想這兩個圖像結合起來:

I would like to changed this map to outlines of the coloured areas, so -for example - there would be a red border around everything blue.

Then I would like to fill the map above like this

這是我寫到目前爲止,在嘗試這一任務的代碼:

# Maping IA, plan 74406IA0010001 

# Importing data 
library(ggplot2) 
library(ggmap) 
library(maps) 
library(mapdata) 
library(stringr) 
library(plyr) 
library(dplyr) 

setwd("/Users/erinmay/Desktop/WL_RA/marketplace2/data") 

county <- map_data("county") 
plan <- read.csv("IA_2017.csv") 

# Using subset 

iowa <- subset(county, region=="iowa") #county point files for iowa 

# Merging in map data 

countyplan <- merge(x=iowa, y=plan, by=c("region","subregion"), all.x=TRUE) 

countyplan <- countyplan[order(countyplan$chosen_plan),] 

# Creating map 

final <- ggplot(data=countyplan) + 
      geom_path(aes(x=long,y=lat,group=RatingArea),colour='black') + 
      geom_polygon(aes(x=long,y=lat,group=group,fill=chosen_plan)) + 
      coord_map() + coord_fixed(1.3) 

ggsave(final,height=6,width=10,unit='in',file='iowa.pdf') 

謝謝您的幫助!

下面是數據: https://www.dropbox.com/s/x8x2l50dvmg0lsb/QHP_IA_2017.csv?dl=0

+0

我們可以有一些樣品數據嗎?這將有助於測試解決方案。請參閱[mcve](https://stackoverflow.com/help/mcve)&[R中的可重現示例](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible -例)。 –

回答

0
基於OP的澄清唯一的顏色各等級區域的 邊界

編輯答案:

據我瞭解,所有多邊形都是平等的ggplot關心的地方。因此,如果必須對多邊形的輪廓進行着色,則無論相鄰兩個多邊形是否屬於同一個評級區域,它都會對所有邊進行着色。

你必須強化它們放入一個數據幀之前在同一規劃區域的溶解多邊形。 (注意:您可以將現有的數據幀轉換回多邊形,但它可能更容易從原始數據源獲取多邊形數據)

library(maps); library(dplyr); library(tidyr); library(maptools); library(rgeos) 

# get map data 
county_map <- map("county", fill = T, plot = FALSE) 

# create mapping table between county names & rating areas 
county_map_match <- data.frame(name = county_map$names) %>% 
    separate(name, c("region", "subregion"), sep = ",", remove = FALSE) %>% 
    left_join(plan %>% select(region, subregion, RatingArea)) 
rownames(county_map_match) <- county_map_match$name 

# convert map to SpatialPolygon, then join with mapping table for SpatialPolygonDataFrame 
county_map <- map2SpatialPolygons(county_map, IDs = county_map$names) 
county_map <- SpatialPolygonsDataFrame(county_map, county_map_match) 

# remove invalidities in the county map 
gIsValid(county_map) #returns FALSE: there are invalid self-intersecting geometries in the polygons, which will cause problems 
county_map <- gBuffer(county_map, byid = TRUE, width = 0) 
gIsValid(county_map) #returns TRUE 

# dissolve county map by rating area & fortify to data frame 
area_map <- unionSpatialPolygons(county_map, IDs = county_map$RatingArea) 
area_map <- fortify(area_map) 
area_map$group <- gsub(".1", "", x= area_map$group, fixed = T) 

一旦你獲得的評價區域中的數據幀的版本,你可以加入入ggplot:

ggplot(countyplan, 
     aes(x=long,y=lat, group = group, fill = chosen_plan)) + 
    geom_polygon(size = 0.5, colour = "black") + 
    geom_polygon(data = area_map, 
      aes(x=long, y=lat, group = group, colour = group), 
      fill = NA, size = 2) + 
    scale_fill_manual(name = "Chosen Plan", values = c("darksalmon"), na.value = "grey") + 
    scale_color_discrete(name = "Rating Area") + 
    coord_map() + coord_fixed(1.3) 

edited ggplot with outer borders coloured

您可以從RColorBrewer包獲得更好的調色板&在scale_XX_brewer()呼叫使用它們,如果你喜歡。單個顏色的名稱可以參考這裏:http://sape.inf.usi.ch/quick-reference/ggplot2/colour

+0

差不多!這非常有幫助,非常感謝。有沒有辦法將評級區域的內部線條改爲黑色,並且只是將評級區域的外部邊框改爲彩色? – Erin

+0

@Erin編輯我的解決方案。 –

相關問題