[R-sig-Geo] ggplot grids polygons, points and lines with sperate legens

Herr, Alexander (L&W, Black Mountain) A|ex@nder@Herr @end|ng |rom c@|ro@@u
Thu May 28 17:10:46 CEST 2020


Hi,

I am unsuccessfully trying to adjust the legend for a multiple feature plot of geom_tile, and geom_sf (with polygons, lines and points).  I would like to have 1) the geom_tile legend separate from the geom_sf polygon legend and also 2) have the legend descriptors ordered descending and 3) each legend for polygon, line and point should have the respective pattern/color, that is rectangle for polygons, lines for lines and shapes for points.

Reproducible code below. Any suggestions?
Thanks
Herry

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
require(sf)
require(sp)

pols<-st_as_sf(spPolygons(rbind(c(1,1),c(1,10),c(10,10),c(10,1),c(1,1))))
p1 =st_sfc((st_polygon(list(rbind(c(0,0), c(0,10), c(10,10), c(10,0), c(0,0))))))
pts<-as.data.frame(st_coordinates(st_make_grid(p1,cellsize=1,what='centers')) )%>%mutate(Value=unlist(lapply(1:10,function(x) sample.int(10,n=3, replace=T))) )
lins<-st_as_sf(rbind(spLines(rbind(c(2,3),c(9,9))),spLines(rbind(c(2,7),c(8,1)))))%>%
          transmute(Line=c("6","7"))
p2<-st_as_sf(spPolygons(rbind(c(1,1),c(9,9),c(9,4),c(1,1))))
p3<-st_as_sf(spPolygons(rbind(c(1,1),c(1,7),c(7,7),c(7,5),c(1,1))))
polys<-st_as_sf(rbind(p2,p3))%>%transmute(Fill=c("4","5"))
ponts<-st_as_sf(pts,coords=c("X","Y") )

ptscols<-c("salmon","light blue","orange")
polycols<-c("dark red","aquamarine")
lncols<-c("deepskyblue","gold")

ggplot()+geom_tile(data=pts, aes(x=X,y=Y,fill=as.character(Value)), show.legend=T )  +
         geom_sf(data=polys, aes(fill=as.character(Fill)), alpha=0.5, size=2,show.legend=T)+
         geom_sf(data=lins, aes(color=Line),legend=T, size=5) +
         geom_sf(data=ponts, aes(shape=as.character(Value)), size=10, show.legend=T)+
         scale_fill_manual("Grid",values=c(ptscols,polycols),
              breaks = c(as.character(unique(pts$Value)),polys$Fill),
                        guide = guide_legend(overrid.aes=list(linetype = NULL, shape=NULL))) +
              scale_color_manual("Lines",values=lncols, breaks = lins$Line,
                        guide = guide_legend(override.aes = list(linetype = "solid", size=5)))   +
              scale_shape_manual("Points",values=unique(as.character(ponts$Value)), breaks = ponts$Value,
                        guide = guide_legend(override.aes = list(size=10)))


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list