2017-11-27 12 views
0

북한 군에 의한 강력 범죄를 측정 R에 choropleth 만들기 :내가</p> <p>내 데이터 세트가 작은 다음과 같다 노스 캐롤라이나 카운티에 의한 강력 범죄를 시각화 할 캐롤라이나

소 지역의 violent_crime을

alamance 396.39 

alexander 130.38 

alleghany 137.48 

anson 513.65 

ashe 78.32 

avery 138.51 

beaufort 328.74 

... 

여기 내 코드가 있습니다 - 지금까지 노스 캐롤라이나와 그 카운티 라인의지도 만 시각화하고 있습니다. 막 다른 골목

... 

library(plotly) 

library(ggplot2) 

library(maps) 

library(dplyr) 

crime.df <- read.csv(file="B:/Data/visualization/violent_crimes.csv", header=TRUE, sep=",") 


vcdExtra::datasets 


nc <- subset(states, region == "north carolina") 

head(nc) 

counties <- map_data("county") 

nc_county <- subset(counties, region == "north carolina") 

head(nc_county) 


choropleth <- inner_join(nc_county, crime.df, by = "subregion") 

choropleth <- chloropleth[!duplicated(chloropleth$order),] 

ggplot(data = nc, mapping = aes(x = long, y = lat, group = group)) + 

    coord_fixed(1.2) + 

    geom_polygon(color = "black", fill = "gray") + 

    geom_polygon(data = nc_county, fill = NA, color = "white") + 

    geom_polygon(color = "black", fill = NA) 




... 

덕분에

난 단지 ggplot 및지도를 사용하려고 시도했지만 내가 실행 해요!

답변

1
library(ggplot2) 
library(dplyr) 

# Get NC counties 

nc_map <- tbl_df(map_data("county", region = "north carolina")) 

# Simulate data since you didn't use dput() as the R section of SO instructs you to do 

set.seed(1492) 
data_frame(
    subregion = unique(nc_map$subregion), 
    crime = sample(50:500, length(unique(nc_map$subregion))) 
) -> crime_df 

# Join the values to the map 

nc_map <- left_join(nc_map, crime_df) 

# Plot it 

ggplot() + 
    geom_polygon(data=nc_map, color="black", 
       aes(x=long, y=lat, group=subregion, fill=crime)) + 
    viridis::scale_fill_viridis(name="Crime ", direction=-1) + 
    coord_map("polyconic") + 
    ggthemes::theme_map() + 
    theme(legend.position="bottom") 

enter image description here

생각해 보면 집중하고 있기 때문에

  • 비닝을 보장 ~ 5 개 그룹
  • 에 범죄 데이터를 일인당 정보를 사용하고 (카운티 인구를 기반으로 on NC)
  • dput()을 SO R 섹션으로 사용하면 데이터를 포함 할 수 있습니다.
  • 오류를 찾아 내고 더 나은 오류 설명을 제공하려고 시도합니다.
  • 투영법을 다시 읽고이 PROJ.4 문자열로 ggalt::coord_proj()을 사용해보십시오. +proj=aea +lat_1=34.0207760236743 +lat_2=36.37811477607033 +lon_0=-80.716552734375 대 내 게으른 방법.
+0

정말 고마워요! 나는 여전히 R에 익숙하지 않고 코드가 내 코드와 크게 다르다. 나는 그 차이를 제게 설명 할 수있을 것이라고 생각하지 않습니까? – PLaks

+1

코드가 실제로 다르지는 않지만 데이터를 포함하지 않았습니다. (대답에서 말했듯이,'dput() '을 사용하여 작업을 재현성있게 만드는 것이 좋습니다) 그래서 관련성이 높습니다 둘. 실제로는지도 데이터를 같은 방식으로 얻고 (대부분) 같은 날 (대부분) 범죄 데이터에 가입하고 플롯팅 코드를 약간 단순화했습니다 (더 나은지도 투영, 색상 표 및 테마를 추가하는 동안) – hrbrmstr