2015-12-08 40 views
5

我使用library(choroplethr)一些市場分析和我有讓我county_choropleth,要麼覆蓋其上ggmap()頂部或在我的代碼使用reference_map=TRUE一些問題。我想要做的就是帶上我的縣長,並將狀態州際公路和州/圈繪製成圓圈/半徑。道路和半徑的圓,在choroplethr,ggmap,或GGPLOT2

我現在有是這樣的:

library(choroplethr) 
data <- Data.frame(County.FIPS = c(19153,19163,19153,19153,19153,19153,19153,19113,19007,19169), Score=c(812.6,769.5,757.9,757.2,722.6,712.4,69727,690.2,64539,642.5) 
county <-aggregate(data$Score~data$County.FIPS,data=data,sum) 
colnames(county) <- c("region", "value") 
mp <- county_choropleth(county, state_zoom=c("iowa"), num_colors = 1) + 
    theme(legend.position="none")+ 
    scale_fill_gradient2("Score", 
         high = "dark green", 
         low = "red", 
         na.value = "grey90", 
         breaks = pretty(county$value, n = 10), 
         label = scales::dollar_format()) 

......這給了我這個陰謀。 enter image description here

從這裏,我想這樣做是覆蓋在我的地圖頂部在愛荷華州的主要州際公路,也創造了一些半徑的圓顯示在英里某些城市的距離。我想它從這個地圖需要的元素和理想它們納入我的choroplethr地圖,因爲,在我看來,它看起來比這個例子更清潔了很多:enter image description here

我用這個代碼來檢索第二張圖:

library(ggmap) 
test<-get_map(location = c(lon=-93.57217,lat=41.67269), maptype="roadmap",source="google",zoom=7,scale="auto") 
yup <- data.frame(lon=c(-93.57217,-95.87509), lat=c(41.67269,41.23238),score=c(1,1)) 
ggmap(test) + stat_density2d(aes(x = lon, y = lat, fill = score,alpha=score), 
       size = 2, bins = 2, data = yup, geom = "polygon") + 
    theme(legend.position="none") 

我與在choroplethr庫使用reference_map=TRUE主要問題是,它呈灰色標籤,道路等,當我把我的county_choropleth在它的上面。 enter image description here

那麼,是否有一個簡單的解決方法,包括在地圖上的道路和繪圖圈,還是我需要放棄使用choroplethr並移動到ggmap,ggplot2或其他?我也可以在他們的網站上找到道路的愛荷華州DOT形狀文件,所以這是一個包含的選項,但我不知道在繪圖和閱讀R時如何使用主要州際公路/高速公路。

這裏是我的「理想」 MS畫圖解決了這個問題:enter image description here

預先感謝您的任何和所有幫助,請讓我知道,如果你有需要,爲了回答任何澄清的問題幫幫我!

+1

我沒有給你一個答案,但我認爲,傳單會允許你這樣做所有你想要的。輸出是一個html文檔,但你可以拍一張快照 – MLavoie

+0

因此,看單張,我已經能夠拿出[this](https://i.gyazo.com/d4ab07e7901a6081898b9d1ae28f8d0f.png)到目前爲止。那麼我的問題就變成了如何按照上面所做的方式去貼上這些縣的標籤?我是否需要使用形狀文件,還是可以像以前一樣使用縣FIPS?順便說一下,傳單是一個很好的工具,我不能感謝你讓我接受它! – medavis6

+1

不錯!嘗試添加一個彈出窗口https://rstudio.github.io/leaflet/shapes.html – MLavoie

回答

2

對於那些後來偶然發現。我能夠通過將庫更改爲leaflettigris來實現我希望做的。

enter image description here

我打算做最後的調整供個人使用的,但這裏是使用的代碼:

library(tigris) 
library(leaflet) 

data <- data.frame(County.FIPS = c(19153,19163,19153,19153,19153,19153,19153,19113,19007,19169), Score=c(812.6,769.5,757.9,757.2,722.6,712.4,69727,690.2,64539,642.5)) 
county <-aggregate(data$Score~data$County.FIPS,data=data,sum) 
colnames(county) <- c("GEOID", "Score") 

IA_counties <- counties(state="IA", cb=TRUE, resolution ="20m") 
IA_merged <- geo_join(IA_counties,county,"GEOID", "GEOID") 
pal <- colorQuantile("Greens",NULL,n=3) 
popup <- paste0("Profitability: ", as.character(IA_merged$Score)) 

yup2 <- data.frame(lon=c(-93.57217,-95.93779),lat=c(41.67269,41.25861),score=c(1,1)) 

leaflet() %>% 
    addProviderTiles("Esri.WorldStreetMap") %>% 
     addLegend(pal = pal, 
     values = IA_merged$Score, 
     position = "bottomright", 
     title = "County Profitablity: ") %>% 
    addCircles(lng=yup2$lon, lat=yup2$lat,weight=1,fillOpacity=0.05,color="red", 
     radius = 96560) %>% 
    addCircles(lng=yup2$lon, lat=yup2$lat,weight=1,fillOpacity=0.025,color="blue", 
     radius = 193121) %>% 
    addPolygons(data = IA_counties, 
     fillColor = ~pal(IA_merged$Score), 
     fillOpacity = 0.15, 
     weight = 0.2, 
     popup = popup)