, , , , @MrFlick, .
, , ( , , ). , :
set.seed(8675309)
df <-
data.frame(
x = runif(40, 1, 20)
, y = runif(40, 100, 140)
, ind = sample(LETTERS[1:4], 40, TRUE)
)
facet_grid
:
ggplot(df , aes(x = x, y = y)) +
geom_point() +
facet_wrap(~ind) +
coord_fixed()
:
, facet_wrap
. , , , , ( , dplyr
):
modDF <-
df %>%
mutate(x = x + as.numeric(ind)*10
, y = y + as.numeric(ind)*20)
, ( modDF
df
)
ggplot(modDF, aes(x = x, y = y)) +
geom_point() +
facet_wrap(~ind) +
coord_fixed()
:
. , , , . , ( ), . dplyr
group_by
x/y. , , , , , /, , , , expand = FALSE
, .
getRanges <-
modDF %>%
group_by(ind) %>%
summarise(
minx = min(x)
, maxx = max(x)
, miny = min(y)
, maxy = max(y)
) %>%
mutate(
midx = (maxx + minx)/2
, midy = (maxy + miny)/2
, xrange = maxx - minx
, yrange = maxy - miny
, xstart = midx - max(xrange)/2 - 0.5
, xend = midx + max(xrange)/2 + 0.5
, ystart = midy - max(yrange)/2 - 0.5
, yend = midy + max(yrange)/2 + 0.5
)
ind minx maxx miny maxy midx midy xrange yrange xstart xend ystart yend
<fctr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A 14.91873 29.53871 120.0743 157.6944 22.22872 138.8844 14.61997 37.62010 14.17717 30.28027 119.5743 158.1944
2 B 22.50432 37.27647 153.5654 179.0589 29.89039 166.3122 14.77215 25.49352 21.83884 37.94195 147.0021 185.6222
3 C 32.15187 47.08845 165.9829 195.0261 39.62016 180.5045 14.93658 29.04320 31.56861 47.67171 161.1945 199.8146
4 D 44.49392 59.59702 192.7243 214.5523 52.04547 203.6383 15.10310 21.82806 43.99392 60.09702 184.3283 222.9484
, , . ( ggtitle
facet_wrap
, strip
facet_wrap
.)
sepPlots <- lapply(levels(modDF$ind), function(thisInd){
thisRange <-
filter(getRanges, ind == thisInd)
modDF %>%
filter(ind == thisInd) %>%
ggplot(aes(x = x, y = y)) +
geom_point() +
coord_fixed(
xlim = c(thisRange$xstart, thisRange$xend)
, ylim = c(thisRange$ystart, thisRange$yend)
, expand = FALSE
) +
facet_wrap(~ind)
})
plot_grid
cowplot
, . , cowplot
. , , cowplot
library(cowplot)
theme_set(theme_gray())
plot_grid(plotlist = sepPlots)
:
, .