Different legends and fill colours for facetted ggplot?

后端 未结 4 2027
野趣味
野趣味 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: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

提交回复
热议问题