How to save continuous axis scaling in the ggplot2 graph grid with different canvas sizes

EDIT: with description, clarified and sample code, graphics added.

I have a dataset with the locations of several animals.

I created a grid of locations for the dispersion of terrain for each animal. Since the xy of the graphic is the distance, I want to keep xy at the same scale for each plot (so there is no distortion in the distance) and on the graphic (so I can compare different graphics with the same scale).

Facet is the natural choice for this, and it works with coord_fixed(). However, it has become more complex when there are outliers in the data (which may be errors). I modified @Mark Peterson's excellent answer to add some outlier points.

set.seed(8675309)
df <-
  data.frame(
    x = runif(40, 1, 20)
    , y = runif(40, 100, 140)
    , ind = sample(LETTERS[1:4], 40, TRUE)
  )
# add some outliers to stretch the plot
outliers <- data.frame(x = c(-100, 30, 60,-50),
                       y = c(20, 200, -100, 500),
                       ind = LETTERS[1:4])
df <- rbind(df, outliers)

ggplot(df , aes(x = x, y = y)) +
  geom_point() +
  facet_wrap(~ind) +
  coord_fixed()

This is what we got. facet with outlier

1.facet plot with coord_fixed(): ,

, , .. xlim ylim . , .

. , , , . , xlim ylim, . , gridExtra cowplot.

, , , ( Shiny).

. , , , - .

, , @Mark Peterson.

, 2D-, . , .

expand_1D_center <- function(vec){
  center <- median(vec)
  new_diff <- max(center - min(vec), 
                  max(vec) - center)
  return(c(new_min = center - new_diff, 
           new_max = center + new_diff))
}
# given x y vectors, get new x y lim to make centroid center
expand_2D_center <- function(x_vec, y_vec){
  return(list(xlim = expand_1D_center(x_vec),
              ylim = expand_1D_center(y_vec)))
}
# plot each with center adjusted
id_vector <- sort(unique(df$ind))
g_list <- vector("list", length = length(id_vector))
for (i in seq_along(id_vector)) {
  data_i <- df[df$ind == id_vector[i], ]
  new_lim <- expand_2D_center(data_i$x, data_i$y)
  g_list[[i]] <- ggplot(data = data_i, aes(x, y)) +
    geom_point() +
    coord_fixed(xlim = new_lim$xlim, ylim = new_lim$ylim) 
}
grid.arrange(grobs = g_list, ncol = 2, respect=TRUE)

center adjust

2. , xy , .

, . , , .

@Mark Peterson , , , .

!

EDIT: , :

, ,

overview graphs

, .

facet

, .

no graphics configured

, . . , .

enter image description here

EDIT: @Mark Peterson , , . , , .

max xrange , . , . , .

+1
1

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

:

enter image description here

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

:

enter image description here

. , , , . , ( ), . 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(
    # Find mid points for range setting
    midx = (maxx + minx)/2
    , midy = (maxy + miny)/2
    # Find size of all ranges
    , xrange = maxx - minx
    , yrange = maxy - miny
    # Set X lims the size of the biggest range, centered at the middle
    , xstart = midx - max(xrange)/2 - 0.5
    , xend = midx + max(xrange)/2 + 0.5
    # Set Y lims the size of the biggest range, centered at the middle
    , 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
    ) +
    # ggtitle(thisInd)
    facet_wrap(~ind)
})

plot_grid cowplot, . , cowplot . , , cowplot

library(cowplot)
theme_set(theme_gray())

plot_grid(plotlist = sepPlots)

:

enter image description here

, .

+3

Source: https://habr.com/ru/post/1017165/


All Articles