i'm trying understand why outputs of facet_grid()
, facet_wrap()
different, though inputs 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()
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, argument scales = "free"
not have same behaviors facet_grid()
, facet_wrap()
. can explain that?
referring this link:
facet_grid
split data facets 1 or 2 variables vary on horizontal and/or vertical direction, while facet_wrap
places facets next each other, wrapping number of columns or rows. in other words, facet_wrap
has horizontal dimension.
therefore, using example link, sp + facet_grid(. ~ sex)
behave same sp + facet_grid( ~ sex)
. in case, facet_grid(. ~ referencia)
, facet_wrap( ~ referencia)
should produce same plot.
for 2 or more dimensional facets, facet_grid
produces grid of plots based on parameter (vertical ~ horizontal)
. facet_wrap
, on other hand, stack plots horizontally. user can set layout specifying number of columns or rows.
now, when scales = "free"
argument added, facets in facet_grid
still bounded grid, therefore plots on same row cannot have different y-axis. similarly, there can single x-axis each column. using facet_wrap
though, each plot displayed independently, can "free" x-axis , y-axis.
in opinion, facet_grid
useful when want relatively compare plots within category, can accomplished setting same axis scales. meanwhile, facet_wrap
more useful plots more independent between 1 another.