2017-12-19 5 views
0

ggplot을 사용하여 두 개의 레이어가있는면 처리 된 맵을 만들려고합니다.ggplot : 다중 레이어가있는면 처리 된 맵

head(poijoin) 

NAME       LAT  LNG  level1 
ATM Bank Of America ATM @ WHC 38.92825 -77.01517 ShopService       
ATM Bank Of America ATM  38.90577 -77.03654 ShopService 
ATM Bank of America   38.91512 -77.02184 ShopService  
ATM USAA ATM @CVS    38.91343 -77.03590 ShopService 
ATM Bank of America ATM  38.95511 -77.02473 ShopService 

다른 하나는 거리 네트워크 모양 파일에서 오는하기, dataframe로 바뀌 fortify을 사용 :

head(streets_df) 

     LNG  LAT order piece id group 
1 -77.02704 38.90253  1  1 0 0.1 
2 -77.02704 38.90303  2  1 0 0.1 
3 -77.02704 38.90303  3  1 0 0.1 
4 -77.02704 38.90304  4  1 0 0.1 
5 -77.02704 38.90326  5  1 0 0.1 

하나는 CSV에서 생성 된 위도/경도 좌표를 포함하는 데이터 프레임 내지 개별적으로 세우고 잘, 그래서 그들을 오버레이 않는 작품 :

ggplot() + 
    geom_point(data = poijoin, aes(x = poijoin$LNG, y = poijoin$LAT)) + 
    geom_path(data = streets_df, aes(x = LNG, y = LAT, group = group)) 

Overlay

,691,363 (210)

(아주 예쁜하지, 나도 알아,하지만 난 여기에 코드의 기본적인 부분에 충실하려고 해요.)

가 나는 또한 문제없이 포인트 층을 패싯 수 있습니다

ggplot() + 
    geom_point(data = poijoin, aes(x = poijoin$LNG, y = poijoin$LAT)) + 
    facet_wrap(~ poijoin$level1, nrow=3) 

facet

그러나 거리를 기본 패싯으로 추가하여 각 패싯에 추가하려면 막혔습니다. 이 :

ggplot() + 
    geom_point(data = poijoin, aes(x = poijoin$LNG, y = poijoin$LAT, color = poijoin$level1)) + 
    geom_path(data = streets_df, aes(x = LNG, y = LAT, group = group)) + 
    facet_wrap(~ poijoin$level1, nrow=3) 

저를 제공합니다 :

Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(8L, 8L, 8L, 8L, : 
    replacement has 20983 rows, data has 200205 

나는 두 개의 데이터 프레임 요소의 다른 번호 (20983 점 200205 거리)를 가지고 있기 때문에 오류가납니다 확신하지만, 아직도 내가 돈 내가 뭘 잘못하고 있는지 알아봐. 어떤 포인터 감사!

st_map <- map_data("state") 

3 개 그룹과 몇 가지 포인트를 확인 :

library(hrbrthemes) 
library(tidyverse) 

지도를 받기 :

+0

내 대답보기 나는 당신의 문제가'~ poijoin $ level1'에 있음을 확신합니다. – hrbrmstr

답변

1

의가 재현 예를 만들자

set.seed(2017-12-19) 
data_frame(
    lat = sample(st_map$lat, 30), 
    lng = sample(st_map$long, 30), 
    level = rep(c("A", "B", "C"), 10) 
) -> points_df 

ggplot() + 
    geom_path(data=st_map, aes(long, lat, group=group), size=0.25) + # basemap 
    geom_point(data=points_df, aes(lng, lat, color=level)) +   # add our points layer 
    coord_map("polyconic") +           # for funsies 
    facet_wrap(~level) +            # NEVER use a fully qualified column unless you know what you're doing 
    labs(x=NULL, y=NULL) + 
    theme_ipsum(grid="") + 
    theme(axis.text=element_blank()) + 
    theme(legend.position="none") 

enter image description here

GG plot2는 패싯을 가로 지르는 기본 맵을 적용한 다음 level 카테고리가있는 다른 레이어에 패싯을 적용합니다.

+0

고마워요! 실제로 완전한 열 이름이었습니다. – Carsten