2017-09-23 4 views
1

R에서 전단지를 만들고 특정 영역에 특정 데이터를 지정하고 싶습니다. 지금 당장 나는 자리 표시자를 사용하여 그것을 알아 내려고 노력하고 있지만 운이 좋지는 않습니다. 특정 카운티에 할당하려는 Excel 파일의 특정 데이터가 있습니다. 나는 어떻게 그것에 대해 갈 것인가?특정 데이터를 R 전단지의 특정 영역에 할당하는 방법

library(maptools) 
library(leaflet) 
library(rjson) 
library(magrittr) 
library(sf) 
library(xlsx) 



## reads in the JSON data of all counties in USA 
counties <- sf::read_sf("http://eric.clst.org/wupl/Stuff/gz_2010_us_050_00_500k.json") 

## selects kansas and missouri county lines 
kscounties<-counties[counties$STATE=="20",] 
mocounties<-counties[counties$STATE=="29",] 


## variable containing all kansas and missouricounty names 
kscountynames<-kscounties$NAME 
mocountynames<-mocounties$NAME 


## combines both counties 
bothcounties<-rbind(kscounties,mocounties) 
bothcountynames<-c(kscountynames,mocountynames) 




## color pallette 
pal<-colorNumeric("viridis",NULL) 

## placeholder 
percent=c(1:100) 


## creates leaflet of kansas and missouri counties 
leaflet(bothcounties) %>% 
    addTiles() %>% 
    addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1, 
    fillColor = ~pal(percent), 
    label = ~paste(bothcountynames, "\n", formatC(percent, big.mark = ",") 
        )) %>% 

     setView(-98.4,38.5,zoom=6) %>% 
     addLegend(position="bottomright",pal = pal, values = percent, opacity = 1.0,title="Percent") %>% 
    addLayersControl(position="topleft", 
        baseGroups = c("1","2"), 
       overlayGroups=c("A","B","C","D","E","F") 

)

답변

1

당신은 문제의 데이터를 포함하는 별도의 dataframe를 작성하고 플롯 할 때 참조 할 수 있습니다. 데이터 프레임의 카운티 순서가 지리 데이터의 카운티 순서와 일치하도록주의를 기울여야합니다. 이미 추출한 카운티 이름은 해당 주문과 일치하는 '키'로 작동해야합니다. 위의 코드에서

,에 #placeholder 부분을 변경 ..

data_to_plot <- data.frame("NAME"=bothcountynames,"data"=###YOURDATA_IN_CORRECT_ORDER###)) 

당신이 플롯 할 데이터 ..with. 또한 이름이있는 단일 열 데이터 프레임을 설정 한 다음 필요한 순서를 유지하는 쉬운 방법 일 수 있으므로 병합/결합을 수행 할 수도 있습니다.

리플릿 호출에서 fillColor = pal(data_to_plot$data)을 입력하십시오. 참조하는 데이터가 별도의 객체에 저장되어있는 경우 ~은 기본적으로 필요하지 않습니다.