What's the difference between facet_wrap() and facet_grid() in ggplot2?

前端 未结 4 1214
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 01:31

I\'ve been reading the ggplot2 documentation for both functions. I was wondering what were the differences and what would be right situation for using each func

4条回答
  •  天命终不由人
    2020-12-13 01:54

    The answer below refers to the case when you have 2 arguments in facet_grid() or facet_wrap().

    facet_grid(x ~ y) will display x*y plots even if some plots are empty. Ex:

    library(ggplot2)
    g <- ggplot(mpg, aes(displ, hwy))
    

    There are 4 distinct cyl and 7 distinct class values.

    g + geom_point(alpha=1/3) + facet_grid(cyl~class)
    

    The above displays 4 * 7 = 28 plots, even if some are empty (because some classes do not have corresponding cylinder values, like rows with class="midsize" doesn't have any corresponding cyl="5" value ) facet_wrap(x ~ y) on the other hand, displays only the plots having actual values.

    g + geom_point(alpha=1/3) + facet_wrap(cyl~class)
    

    There are 19 plots displayed now, one for every combination of cyl and class.

提交回复
热议问题