简体   繁体   English

比例=“免费”适用于facet_wrap,但不适用于facet_grid

[英]Scales = “free” works for facet_wrap but doesn't for facet_grid

I'm trying to understand why the outputs of facet_grid() and facet_wrap() are different, even though the inputs are the same: 我试图了解为何facet_grid()facet_wrap()的输出不同,即使输入相同:

facet_grid facet_grid

ggplot(temp, aes(x = valor)) +
geom_histogram(binwidth = 5, fill = NA, color = "black") +
facet_grid(estratificacao ~ referencia, scales = "free") +
scale_x_continuous(breaks = seq(0, 100, 10)) + theme_classic()

在此处输入图片说明

facet_wrap facet_wrap

ggplot(temp, aes(x = valor)) +
geom_histogram(binwidth = 5, fill = NA, color = "black") +
facet_wrap(estratificacao ~ referencia, scales = "free") +
scale_x_continuous(breaks = seq(0, 100, 10)) + theme_classic()

在此处输入图片说明

See, the argument scales = "free" does not have the same behaviors for facet_grid() and facet_wrap() . 参见, scales = "free"参数对于facet_grid()facet_wrap() What can explain that? 有什么可以解释的?

Referring to this link : 引用此链接

facet_grid split the data into facets by one or two variables that vary on the horizontal and/or vertical direction, while facet_wrap places the facets next to each other, wrapping with a certain number of columns or rows. facet_grid通过在水平和/或垂直方向上变化的一个或两个变量将数据划分为多个方面,而facet_wrap这些方面彼此相邻,并包裹了一定数量的列或行。 In other words, facet_wrap only has horizontal dimension. 换句话说, facet_wrap仅具有水平尺寸。

Therefore, using the example from that link, sp + facet_grid(. ~ sex) would behave the same as sp + facet_grid( ~ sex) . 因此,使用该链接中的示例, sp + facet_grid(. ~ sex)行为与sp + facet_grid( ~ sex) In your case, facet_grid(. ~ referencia) and facet_wrap( ~ referencia) should produce the same plot. 在您的情况下, facet_grid(. ~ referencia)facet_wrap( ~ referencia)应该产生相同的图。

For two or more dimensional facets, facet_grid produces a grid of plots based on the parameter (vertical ~ horizontal) . 对于二维或更多个小平面, facet_grid基于参数(vertical ~ horizontal)生成一个网格图。 facet_wrap , on the other hand, would just stack the plots horizontally. 另一方面, facet_wrap只会将绘图水平堆叠。 User then can set the layout by specifying the number of columns or rows. 然后,用户可以通过指定列数或行数来设置布局。

Now, when the scales = "free" argument is added, facets in facet_grid would still be bounded by the grid, therefore plots on the same row cannot have different y-axis. 现在,当添加scales = "free"参数时, facet_grid构面仍将受到网格的限制,因此同一行上的图不能具有不同的y轴。 Similarly, there can only single x-axis for each column. 同样,每列只能有一个x轴。 Using facet_wrap though, each plot is displayed independently, so it can "free" its x-axis and y-axis. 但是,使用facet_wrap可以独立显示每个图,因此可以“释放”其x轴和y轴。

In my opinion, facet_grid is useful when you want to relatively compare the plots within a category, which can be accomplished by setting the same axis scales. 我认为,当您想相对比较类别中的图时, facet_grid很有用,这可以通过设置相同的轴比例来实现。 Meanwhile, facet_wrap is more useful for plots that more independent between one another. 同时, facet_wrap对于彼此之间更加独立的图更为有用。

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

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