Some sample data:
dfr <- data.frame(
x = rep.int(1:10, 2),
y = runif(20),
g = factor(rep(letters[1:2], each = 10))
)
A simple scatterplot with two facets:
p <- ggplot(dfr, aes(x, y)) +
geom_point() +
facet_wrap(~ g, scales = "free_y")
I can set the axis limits for all panels with
p + scale_y_continuous(limits = c(0.2, 0.8))
(or a wrapper for this like ylim
)
but how do I set different axis limits for different facets?
The latticey way to do it would be to pass a list to this argument, e.g.,
p + scale_y_continuous(limits = list(c(0.2, 0.8), c(0, 0.5)))
Unfortunately that just throws an error in the ggplot2 case.
EDIT:
Here's a partial hack. If you want to extend the range of the scales then you can add columns to your dataset specifying the limits, then draw them with geom_blank
.
Modified dataset:
dfr <- data.frame(
x = rep.int(1:10, 2),
y = runif(20),
g = factor(rep(letters[1:2], each = 10)),
ymin = rep(c(-0.6, 0.3), each = 10),
ymax = rep(c(1.8, 0.5), each = 10)
)
Updated plot:
p + geom_blank(aes(y = ymin)) + geom_blank(aes(y = ymax))
Now the scales are different and the left hand one is correct. Unfortunately, the right hand scale doesn't contract since it needs to make room for the points.
In case it helps, we can now rephrase the question as "is it possible to draw points without the scales being recalculated and without explicitly calling scale_y_continuous
?"
The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.
To remove the label from facet plot, we need to use “strip. text. x” argument inside the theme() layer with argument 'element_blank()'.
geom_blank.Rd. The blank geom draws nothing, but can be a useful way of ensuring common scales between different plots. See expand_limits() for more details.
Faceting is the process that split the chart window in several small parts (a grid), and display a similar chart in each section. Each section usually shows the same graph for a specific group of the dataset. The result is usually called small multiple.
I don't think this is possible yet in ggplot2. This discussion from January suggests the issue is under consideration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With