简体   繁体   English

ggplot2 中的 facet_wrap() 和 facet_grid() 有什么区别?

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

I've been reading the ggplot2 documentation for both functions.我一直在阅读这两个函数的ggplot2文档。 I was wondering what were the differences and what would be right situation for using each function ( facet_wrap() and facet_grid() ).我想知道有什么区别以及使用每个函数( facet_wrap()facet_grid() )的正确情况是什么。

library(ggplot2)

p <- qplot(displ, hwy, data = mpg)
p + facet_wrap(~ cyl)

p + facet_grid(~ cyl)

I provide this small example to serve as starting point.我提供这个小例子作为起点。 The difference seems to be wrap makes the plots more autonomous and grid makes one plot all together.区别似乎是 wrap 使情节更加自主,而 grid 使一个情节全部在一起。

The answer below refers to the case when you have 2 arguments in facet_grid() or facet_wrap() .下面的答案是指在facet_grid()facet_wrap()有 2 个参数的情况。

facet_grid(x ~ y) will display x*y plots even if some plots are empty.即使某些facet_grid(x ~ y)也会显示x*y图。 Ex:例如:

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

There are 4 distinct cyl and 7 distinct class values.有 4 个不同的 cyl 和 7 个不同的类值。

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.上面显示 4 * 7 = 28 个图,即使有些是空的(因为有些类没有对应的圆柱值,比如 class="midsize" 的行没有任何对应的 cyl="5" 值) facet_wrap(x ~ y)另一方面,仅显示具有实际值的图。

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

There are 19 plots displayed now, one for every combination of cyl and class.现在显示了 19 个图,每个 cyl 和 class 组合一个。

facet_wrap(...) strings together ggplots in different frames (facets) based in a single variable. facet_wrap(...)将基于单个变量的不同帧(构面)中的ggplots串在一起。 facet_grid(...) can take two variables: facet_grid(...)可以采用两个变量:

p + facet_grid(cyl~class)

You can also use a third variable to group in each facet:您还可以使用第三个变量在每个方面进行分组:

qplot(displ, hwy, data=mpg,color=factor(year)) + facet_grid(cyl~class)

shows the improvement (or lack thereof) in hwy mileage vs. displacement by #cylinders and class.显示了高速公路里程与排量的改善(或缺乏)#气缸和等级。

Quoting mainly from ggplot2 book, p.主要引用 ggplot2 book, p. 148f. 148f。

There are three types of facetting:刻面分为三种类型:

  • facet_null() : a single plot, the default. facet_null() :单个图,默认值。
  • facet_wrap() : "wraps" a 1d ribbon of panels into 2d. facet_wrap() :将一facet_wrap()面板带“包裹”成二维。
  • facet_grid() : produces a 2d grid of panels defined by variables which form the rows and columns. facet_grid() :生成由形成行和列的变量定义的二维面板网格。

在此处输入图片说明

Facet wrap刻面环绕

facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. facet_wrap()制作一长条面板(由任意数量的变量生成)并将其包装成 2d。 This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner.如果您有一个具有多个级别的单个变量并希望以更节省空间的方式排列图,这将非常有用。

You can control how the ribbon is wrapped into a grid with ncol , nrow , as.table and dir .您可以控制功能区是如何包裹成一个网格ncolnrowas.tabledir ncol and nrow control how many columns and rows (you only need to set one). ncolnrow控制多少列和行(你只需要设置一个)。 as.table controls whether the facets are laid out like a table ( TRUE ), with highest values at the bottom-right, or a plot ( FALSE ), with the highest values at the top-right. as.table控制构面是像表格 ( TRUE ) 一样布局,最高值在右下角,还是图 ( FALSE ),最高值在右上角。 dir controls the direction of wrap: h orizontal or v ertical. dir控制缠绕的方向:H orizo​​ntal或v ertical。

Facet grid刻面网格

From ?facet_grid : facet_grid() forms a matrix of panels defined by row and column faceting variables.?facet_gridfacet_grid()形成由行和列分面变量定义的面板矩阵。 It is most useful when you have two discrete variables, and all combinations of the variables exist in the data.当您有两个离散变量并且数据中存在变量的所有组合时,它最有用。

You can use multiple variables in the rows or columns, by "adding" them together, eg a + b ~ c + d .您可以在行或列中使用多个变量,通过将它们“添加”在一起,例如a + b ~ c + d

facet grid() has an additional parameter called space , which takes the same values as scales . facet grid()有一个名为space的附加参数,它采用与scales相同的值。

# If scales and space are free, then the mapping between position
# and values in the data will be the same across all panels. This
# is particularly useful for categorical axes
ggplot(subset(mpg, manufacturer %in% c("audi", "honda", "toyota")) , aes(drv, model)) +
    geom_point() +
    facet_grid(manufacturer ~ ., scales = "free", space = "free") +
    theme(strip.text.y = element_text(angle = 0))

在此处输入图片说明

( simplified ) Example taken from ?facet_grid (简化)示例取自?facet_grid

For single variable plots you can use either facet_grid() or facet_wrap() .对于单变量图,您可以使用facet_grid()facet_wrap()

facet_wrap(~variable) will return a symmetrical matrix of plots for the number of levels of variable . facet_wrap(~variable)将返回一个关于variable级别数的对称图矩阵。

facet_grid(.~variable) will return facets equal to the levels of variable distributed horizontally. facet_grid(.~variable)将返回与水平分布的variable级别相等的方面。

facet_grid(variable~.) will return facets equal to the levels of variable distributed vertically. facet_grid(variable~.)将返回与垂直分布的variable水平相等的面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM