Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

r - 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

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()

enter image description here

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()

enter image description here

See, the argument scales = "free" does not have the same behaviors for facet_grid() and facet_wrap(). What can explain that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. In other words, facet_wrap only has horizontal dimension.

Therefore, using the example from that link, sp + facet_grid(. ~ sex) would behave the same as sp + facet_grid( ~ sex). In your case, facet_grid(. ~ referencia) and facet_wrap( ~ referencia) should produce the same plot.

For two or more dimensional facets, facet_grid produces a grid of plots based on the parameter (vertical ~ horizontal). facet_wrap, on the other hand, would just stack the plots horizontally. 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. Similarly, there can only single x-axis for each column. Using facet_wrap though, each plot is displayed independently, so it can "free" its x-axis and y-axis.

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. Meanwhile, facet_wrap is more useful for plots that more independent between one another.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...