Different legends and fill colours for facetted ggplot?

后端 未结 4 2029
野趣味
野趣味 2020-11-30 10:26

Sorry for not included any example data for my problem. I couldn’t find a way to easily produce an example shape file. Hopefully, experienced users of ggplot ca

4条回答
  •  一整个雨季
    2020-11-30 10:42

    With grid goodness

    align.plots <- function(..., vertical=TRUE){
    #http://ggextra.googlecode.com/svn/trunk/R/align.r
      dots <- list(...)
      dots <- lapply(dots, ggplotGrob)
      ytitles <- lapply(dots, function(.g) editGrob(getGrob(.g,"axis.title.y.text",grep=TRUE), vp=NULL))
      ylabels <- lapply(dots, function(.g) editGrob(getGrob(.g,"axis.text.y.text",grep=TRUE), vp=NULL))
      legends <- lapply(dots, function(.g) if(!is.null(.g$children$legends))
                        editGrob(.g$children$legends, vp=NULL) else ggplot2:::.zeroGrob)
    
      gl <- grid.layout(nrow=length(dots))
      vp <- viewport(layout=gl)
      pushViewport(vp)
      widths.left <- mapply(`+`, e1=lapply(ytitles, grobWidth),
                            e2= lapply(ylabels, grobWidth), SIMPLIFY=F)
      widths.right <- lapply(legends, function(g) grobWidth(g) + if(is.zero(g)) unit(0, "lines") else unit(0.5, "lines")) # safe margin recently added to ggplot2
      widths.left.max <- max(do.call(unit.c, widths.left))
      widths.right.max <- max(do.call(unit.c, widths.right))
    
      for(ii in seq_along(dots)){
        pushViewport(viewport(layout.pos.row=ii))
        pushViewport(viewport(x=unit(0, "npc") + widths.left.max - widths.left[[ii]],
                              width=unit(1, "npc") - widths.left.max + widths.left[[ii]] -
                                                     widths.right.max + widths.right[[ii]],
                              just="left"))
        grid.draw(dots[[ii]])
      upViewport(2)
      }
    }
    
    
    
    p <- ggplot(datapoly[datapoly$variable=="val1",], aes(x=x, y=y)) + geom_polygon(aes(fill=value, group=id),colour="black")
    p1 <- ggplot(datapoly[datapoly$variable=="val2",], aes(x=x, y=y)) + geom_polygon(aes(fill=value, group=id),colour="black")
    align.plots( p,p1)
    

提交回复
热议问题