i'm trying build map in ggplot2 using data separate data frames.
library(maptools) xx <- readshapepoly(system.file("shapes/sids.shp", package="maptools")[1], idvar="fipsno", proj4string=crs("+proj=longlat +ellps=clrk66")) xx.sub1 <- subset(xx, xx$fipsno < 37010) xx.sub2 <- subset(xx, xx$fipsno > 37010) xx.sub1@data$id <- rownames(xx.sub1@data) xx.sub1.points <- fortify(xx.sub1, region="id") xx.sub1.df = join(xx.sub1.points, xx.sub1@data, by="id") xx.sub2@data$id <- rownames(xx.sub2@data) xx.sub2.points <- fortify(xx.sub2, region="id") xx.sub2.df = join(xx.sub2.points, xx.sub2@data, by="id") ggplot(xx.sub2.df) + aes(long, lat, fill = (sid79/bir79)*1000, group = group) + geom_polygon() + geom_path(color="grey80") + coord_equal() + scale_fill_gradientn(colours = brewer.pal(7, "ylorbr")) + geom_polygon(data = xx.sub1.df, fill = "grey50") + geom_path(data = xx.sub1.df, color="grey80") + labs(fill = "mapped value", title = "title") up point works expected , nice map:

what i'd change add separate legend data xx.sub1.df - since polygons filled grey hope 1 additional entry.
how can achieve that?
i'm not 100% sure want, here's how i'd approach problem understand it. if map unused geom data xx.sub1.df, make invisible on plot, can still legend geom. here i've used geom_point, make others.
p <- ggplot(xx.sub2.df) + aes(long, lat, fill = (sid79/bir79)*1000, group = group) + geom_polygon() + geom_path(color="grey80") + coord_equal() + scale_fill_gradientn(colours = brewer.pal(7, "ylorbr")) + geom_polygon(data = xx.sub1.df, fill = "grey50") + geom_path(data = xx.sub1.df, color="grey80") + labs(fill = "mapped value", title = "title") #now add geom_point() setting shape na, colour "grey50", #legend displaying right colour p2 <- p + geom_point(data = xx.sub1.df, aes(size="xx.sub1", shape = na), colour = "grey50") 
now need alter size , shape of point on legend, , change name of legend (thanks @diziselferts demonstrated earlier).
p2 + guides(size=guide_legend("source", override.aes=list(shape=15, size = 10))) 
of course can change way labels work or whatever highlight want show.
if isn't you're after, let me know!
Comments
Post a Comment