Different legends and fill colours for facetted ggplot?

后端 未结 4 2010
野趣味
野趣味 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)
    
    0 讨论(0)
  • 2020-11-30 10:43

    Currently there can be only one scale per plot (for everything except x and y).

    0 讨论(0)
  • 2020-11-30 10:53

    Perhaps a little unorthodox, but you could try factoring your "value". For example:

    p <- ggplot(datapoly, aes(x=x, y=y)) + geom_polygon(aes(fill=factor(value), group=id),colour="black")
    p <- p + facet_wrap(~ variable)
    p
    

    ggplot2 uses factors to create legends. So if you could add a column that takes "value" and breaks it into factored ranges, you could replace "value" with the ranges.

    Create a column, like "f":

        id variable        value   x    y f
    1  1.1     val1   0.09838607 2.0 -0.5 0.09-0.13
    2  1.1     val1   0.09838607 1.0  0.0 0.09-0.13
    3  1.1     val1   0.09838607 1.1  1.0 0.09-0.13
    4  1.1     val1   0.09838607 2.2  0.5 0.09-0.13
    25 2.1     val1   0.13121347 1.0  0.0 0.13-0.20
    

    ...

    Then use:

    p <- ggplot(datapoly, aes(x=x, y=y)) + geom_polygon(aes(fill=f, group=id),colour="black")
    p <- p + facet_wrap(~ variable)
    p
    

    You'd have to specify the categories that you want, which could be time consuming. But at least the graph would come out how you want it to. Basically, you'd be recoding the data into another column. Here are some examples:

    http://www.statmethods.net/management/variables.html

    0 讨论(0)
  • 2020-11-30 10:55

    At the risk of stating the obvious, it seems like you should be coloring by percents instead of raw values. Then your transformed values and your legend go from 0 to 1.

    0 讨论(0)
提交回复
热议问题