Skip to content

Fixed coordinates #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
first stab
  • Loading branch information
cpsievert committed Mar 14, 2016
commit 6e58f480ad4c3f98adb31de73f10d8f21c1741a0
11 changes: 10 additions & 1 deletion R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,16 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
rep(panelMarginY, 2)
)

doms <- get_domains(nPanels, nRows, margins)
# if fixed coordinates, add to the plot margin
if (inherits(p$coordinates, "CoordFixed") &&
!isTRUE(Reduce(`&&`, p$facet$free))) {
rng <- panel$ranges[[1]]
aspect_ratio <- diff(rng$y.range)/diff(rng$x.range) * p$coordinates$ratio
} else {
aspect_ratio <- 1
}

doms <- get_domains(nPanels, nRows, margins, aspect_ratio)

for (i in seq_len(nPanels)) {
lay <- panel$layout[i, ]
Expand Down
20 changes: 14 additions & 6 deletions R/subplots.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,25 @@ subplot <- function(..., nrows = 1, which_layout = "merge", margin = 0.02) {
hash_plot(data.frame(), p)
}


get_domains <- function(nplots = 1, nrows = 1, margins = 0.01) {
# aspect ratio (y / x)
get_domains <- function(nplots = 1, nrows = 1, margins = 0.01, aspect = NULL) {
if (length(margins) == 1) margins <- rep(margins, 4)
if (length(margins) != 4) stop("margins must be length 1 or 4", call. = FALSE)
ncols <- ceiling(nplots / nrows)

cushion <- list(x = 0, y = 0)
if (!is.null(aspect) && aspect < 1) {
cushion <- list(x = 0, y = (1 - nrows * aspect) / nrows)
}
if (!is.null(aspect) && aspect >= 1) {
cushion <- list(x = (1 - ncols * 1 / aspect) / ncols, y = 0)
}

xs <- vector("list", ncols)
for (i in seq_len(ncols)) {
xs[[i]] <- c(
xstart = ((i - 1) / ncols) + ifelse(i == 1, 0, margins[1]),
xend = (i / ncols) - ifelse(i == ncols, 0, margins[2])
xstart = ((i - 1) / ncols) + ifelse(i == 1, 0 + cushion$x, margins[1]),
xend = (i / ncols) - ifelse(i == ncols, 0 + cushion$x, margins[2])
)
}
xz <- rep_len(xs, nplots)
Expand All @@ -158,8 +166,8 @@ get_domains <- function(nplots = 1, nrows = 1, margins = 0.01) {
for (i in seq_len(nplots)) {
j <- ceiling(i / ncols)
ys[[i]] <- c(
ystart = 1 - ((j - 1) / nrows) - ifelse(j == 1, 0, margins[3]),
yend = 1 - (j / nrows) + ifelse(j == nrows, 0, margins[4])
ystart = 1 - ((j - 1) / nrows) - ifelse(j == 1, 0 + cushion$y, margins[3]),
yend = 1 - (j / nrows) + ifelse(j == nrows, 0 + cushion$y, margins[4])
)
}
list2df(Map(c, xz, ys))
Expand Down