# Color a layer of the map based on the location of dots on another layer

Thanks for helping me in GIS 101 Problem #2, now I have followup questions:

I created a map as below:

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 map of the Connecticut Census State Legislative District Map with several hospital located, you can see a few regions dot and a few does not. I want to fill the regions with dot using one colour, and regions without dot using another colour. Can anyone give me a clue? Thanks.

-

@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.