简体   繁体   中英

Calculating density within a shapefile

I'm trying to calculate density within a shapefile, but I'm fairly confident I'm doing it wrong. The idea is to figure out which geographical regions there have been the most sales by density.

Here is a link to the file that I use (testdata.shp)

library(sf)

sample <- st_read("testdata.shp")

sample$area <- st_area(sample$geometry)

density_calc <-sample %>% st_buffer(0) %>% group_by(areas) %>% summarise(`Sales (density)` = sum(sales)/sum(area))

Here are the details of the shapefile:

Geometry set for 2106 features 
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -120.0065 ymin: 35.00184 xmax: -114.0396 ymax: 42.00221
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs

I guess my issue is, I don't really know what is right and wrong, so I have no clue if I did it correctly.

Sorry if it's not the most extensive question, I just don't remember my high school geometry that well!

the raster package helps make this calculation very easy and just like working with a data.frame in R :

library(raster)
list.files(workDir)
test_shp <- shapefile(file.path(workDir, 'testdata.shp'))
names(test_shp)
#[1] "distrct"       "sbdstrc"       "terrtry"      
#[4] "region"        "turf"          "sales"        
#[7] "leads"         "cnvrsns"       "areas" 

sum(is.na(test_shp$sales)) #note that 346 polygons have no sales data

#get the area as square kilometers
test_shp$km2 <- area(test_shp) / 10000

#calc the sales density
test_shp$sales_density <- test_shp$sales / test_shp$km2

#calculate the 25th, 50th, and 75th percentile of all polygons
quartiles <- quantile(test_shp$sales_density, probs=c(0.25, 0.5, 0.75), na.rm=TRUE) 

#plot the result, coloring by which percentile the sales density is for a given polygon 
plot(test_shp, col=ifelse(is.na(test_shp$sales_density), 'gray', ifelse(test_shp$sales_density >= quartiles[3], 'dark green', ifelse(test_shp$sales_density >= quartiles[2], 'light green', ifelse(test_shp$sales_density >= quartiles[1], 'yellow', 'red')))), border='transparent')  (eg. >75th, 50-75th, etc.)

#add the legend
legend('bottomleft', legend=c('Q4', 'Q3', 'Q2', 'Q1', 'No data'), pch=15, col=c('dark green', 'light green', 'yellow', 'red', 'gray'))

销售密度结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM