I created a map as below:

enter image description here

The map is created using the following script:

ct <- readShapeSpatial("housect_37800_0000_2010_s100_census_1_shp/wgs84/housect_37800_0000_2010_s100_census_1_shp_wgs84.shp")

ct_mod <- fortify(ct,region="SLDLST10")
chart <- ggplot(data=ct_mod,aes(long,lat))
chart <- chart + scale_x_continuous(limits=c(-73.8,-71.7),breaks=seq(-74,-71,0.1))
chart <- chart + scale_y_continuous(limits=c(40.9,42.1),breaks=seq(40,43,0.1))
chart <- chart + geom_polygon(fill="grey80",aes(group=group))
chart <- chart + geom_path(color="white",aes(group=group))  
# coord_equal make x-y same scale
chart <- chart + coord_equal()

chart1 <- chart + geom_point(data=hosp.list,aes(x=coord_x,y=coord_y))  

This is a map of the Connecticut Census State Legislative District Map with several hospitals located, you can see a few regions with a dot and others without. How can I fill the regions with dot using one colour and regions without dot using another colour?


You could do a point in polygon operation to determine which regions contain a dot. You can then subset the regions that have dots and plot them with a different color.

  • @RK I tried to extract the coordinates of one of the region using the code ct[ct@data[,"NAMELSAD10"]=="State House District 23",]@polygons[[1]]@Polygons[[1]]@coords, but I think I am working it in a very clumsy way, I know nothing about "slot", is there a more elegant way for doing this? – lokheart Mar 4 '12 at 16:16

Quick: use the over(pts,poly) function to return which polygon each point is in. [You might have to construct pts as a SpatialPoints object from your data frame]. Then the return value tells you which polygons have points in. Add a new column to your SpatialPolygonsDataFrame (ct) and then colour it in the usual ggplot way.

Sorry can't say more but there should be enough for you to work it out. See package:sp and the over function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.