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

Bede-Fazekas Ákos b|@|ev||@t @end|ng |rom gm@||@com
Thu May 28 18:17:21 CEST 2020


Hello Harry,
it is not the final solution, just one a part of it:

fill_levels <- unique(c(polys$Fill, pts$Value))
polys$Fill <- factor(x = polys$Fill, levels = fill_levels)
pts$Value <- factor(x = pts$Value, levels = fill_levels)
ponts$Value <- as.factor(ponts$Value)
ggplot()+
     geom_tile(data=pts, aes(x=X,y=Y,fill=Value), show.legend=F) +
     geom_sf(data=polys, aes(fill=Fill), alpha=0.5, 
size=2,show.legend="polygon")+
     geom_sf(data=lins, aes(color=Line),show.legend="line", size=5) +
     geom_sf(data=ponts, aes(shape=Value), size=10, show.legend="point") +
     scale_fill_manual("Grid",values=c(ptscols,polycols)) +
     scale_color_manual("Lines",values=lncols) +
scale_shape_manual("Points",values=as.numeric(levels(ponts$Value))) +
     guides(fill = guide_legend(override.aes = list(linetype = 0, shape 
= 0)), color = guide_legend(override.aes = list(shape = 0, linetype = 
"solid", size=5)), shape = guide_legend(override.aes = list(linetype = 
0, size=10)))

HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences

2020.05.28. 17:10 keltezéssel, Herr, Alexander (L&W, Black Mountain) írta:
> 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]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list