Skip to content

Fix stack and clipping in facets #1789

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

Merged
merged 1 commit into from
Sep 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 13 additions & 13 deletions R/facet-grid-.r
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
}

panel_table <- gtable_matrix("layout", panel_table,
panel_widths, panel_heights, respect = respect)
panel_widths, panel_heights, respect = respect, clip = "on")
panel_table$layout$name <- paste0('panel-', rep(seq_len(ncol), nrow), '-', rep(seq_len(nrow), each = ncol))

panel_table <- gtable_add_col_space(panel_table,
Expand All @@ -327,10 +327,10 @@ FacetGrid <- ggproto("FacetGrid", Facet,
panel_pos_col <- panel_cols(panel_table)
panel_pos_rows <- panel_rows(panel_table)

panel_table <- gtable_add_grob(panel_table, axes$x$top, 1, panel_pos_col$l, clip = "off", name = paste0("axis-t-", seq_along(axes$x$top)))
panel_table <- gtable_add_grob(panel_table, axes$x$bottom, -1, panel_pos_col$l, clip = "off", name = paste0("axis-b-", seq_along(axes$x$bottom)))
panel_table <- gtable_add_grob(panel_table, axes$y$left, panel_pos_rows$t, 1, clip = "off", name = paste0("axis-l-", seq_along(axes$y$left)))
panel_table <- gtable_add_grob(panel_table, axes$y$right, panel_pos_rows$t, -1, clip = "off", name = paste0("axis-r-", seq_along(axes$y$right)))
panel_table <- gtable_add_grob(panel_table, axes$x$top, 1, panel_pos_col$l, clip = "off", name = paste0("axis-t-", seq_along(axes$x$top)), z = 3)
panel_table <- gtable_add_grob(panel_table, axes$x$bottom, -1, panel_pos_col$l, clip = "off", name = paste0("axis-b-", seq_along(axes$x$bottom)), z = 3)
panel_table <- gtable_add_grob(panel_table, axes$y$left, panel_pos_rows$t, 1, clip = "off", name = paste0("axis-l-", seq_along(axes$y$left)), z = 3)
panel_table <- gtable_add_grob(panel_table, axes$y$right, panel_pos_rows$t, -1, clip = "off", name = paste0("axis-r-", seq_along(axes$y$right)), z= 3)

# Add strips
switch_x <- !is.null(params$switch) && params$switch %in% c("both", "x")
Expand All @@ -343,22 +343,22 @@ FacetGrid <- ggproto("FacetGrid", Facet,
if (!is.null(strips$x$bottom)) {
if (inside_x) {
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$bottom), -2)
panel_table <- gtable_add_grob(panel_table, strips$x$bottom, -2, panel_pos_col$l, clip = "off", name = paste0("strip-b-", seq_along(strips$x$bottom)))
panel_table <- gtable_add_grob(panel_table, strips$x$bottom, -2, panel_pos_col$l, clip = "on", name = paste0("strip-b-", seq_along(strips$x$bottom)), z = 2)
} else {
panel_table <- gtable_add_rows(panel_table, strip_padding, -1)
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$bottom), -1)
panel_table <- gtable_add_grob(panel_table, strips$x$bottom, -1, panel_pos_col$l, clip = "off", name = paste0("strip-b-", seq_along(strips$x$bottom)))
panel_table <- gtable_add_grob(panel_table, strips$x$bottom, -1, panel_pos_col$l, clip = "on", name = paste0("strip-b-", seq_along(strips$x$bottom)), z = 2)
}
}
} else {
if (!is.null(strips$x$top)) {
if (inside_x) {
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$top), 1)
panel_table <- gtable_add_grob(panel_table, strips$x$top, 2, panel_pos_col$l, clip = "off", name = paste0("strip-t-", seq_along(strips$x$top)))
panel_table <- gtable_add_grob(panel_table, strips$x$top, 2, panel_pos_col$l, clip = "on", name = paste0("strip-t-", seq_along(strips$x$top)), z = 2)
} else {
panel_table <- gtable_add_rows(panel_table, strip_padding, 0)
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$top), 0)
panel_table <- gtable_add_grob(panel_table, strips$x$top, 1, panel_pos_col$l, clip = "off", name = paste0("strip-t-", seq_along(strips$x$top)))
panel_table <- gtable_add_grob(panel_table, strips$x$top, 1, panel_pos_col$l, clip = "on", name = paste0("strip-t-", seq_along(strips$x$top)), z = 2)
}
}
}
Expand All @@ -367,22 +367,22 @@ FacetGrid <- ggproto("FacetGrid", Facet,
if (!is.null(strips$y$left)) {
if (inside_y) {
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$left), 1)
panel_table <- gtable_add_grob(panel_table, strips$y$left, panel_pos_rows$t, 2, clip = "off", name = paste0("strip-l-", seq_along(strips$y$left)))
panel_table <- gtable_add_grob(panel_table, strips$y$left, panel_pos_rows$t, 2, clip = "on", name = paste0("strip-l-", seq_along(strips$y$left)), z = 2)
} else {
panel_table <- gtable_add_cols(panel_table, strip_padding, 0)
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$left), 0)
panel_table <- gtable_add_grob(panel_table, strips$y$left, panel_pos_rows$t, 1, clip = "off", name = paste0("strip-l-", seq_along(strips$y$left)))
panel_table <- gtable_add_grob(panel_table, strips$y$left, panel_pos_rows$t, 1, clip = "on", name = paste0("strip-l-", seq_along(strips$y$left)), z = 2)
}
}
} else {
if (!is.null(strips$y$right)) {
if (inside_y) {
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$right), -2)
panel_table <- gtable_add_grob(panel_table, strips$y$right, panel_pos_rows$t, -2, clip = "off", name = paste0("strip-r-", seq_along(strips$y$right)))
panel_table <- gtable_add_grob(panel_table, strips$y$right, panel_pos_rows$t, -2, clip = "on", name = paste0("strip-r-", seq_along(strips$y$right)), z = 2)
} else {
panel_table <- gtable_add_cols(panel_table, strip_padding, -1)
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$right), -1)
panel_table <- gtable_add_grob(panel_table, strips$y$right, panel_pos_rows$t, -1, clip = "off", name = paste0("strip-r-", seq_along(strips$y$right)))
panel_table <- gtable_add_grob(panel_table, strips$y$right, panel_pos_rows$t, -1, clip = "on", name = paste0("strip-r-", seq_along(strips$y$right)), z = 2)
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions R/facet-wrap.r
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ FacetWrap <- ggproto("FacetWrap", Facet,
empties <- apply(panel_table, c(1,2), function(x) is.zero(x[[1]]))
panel_table <- gtable_matrix("layout", panel_table,
widths = unit(rep(1, ncol), "null"),
heights = unit(rep(aspect_ratio, nrow), "null"), respect = respect)
heights = unit(rep(aspect_ratio, nrow), "null"), respect = respect, clip = "on")
panel_table$layout$name <- paste0('panel-', rep(seq_len(ncol), nrow), '-', rep(seq_len(nrow), each = ncol))

panel_table <- gtable_add_col_space(panel_table,
Expand Down Expand Up @@ -285,10 +285,10 @@ FacetWrap <- ggproto("FacetWrap", Facet,
axis_mat_y_right[col_pos] <- col_axes
}
}
panel_table <- weave_tables_row(panel_table, axis_mat_x_top, -1, axis_height_top, "axis-t")
panel_table <- weave_tables_row(panel_table, axis_mat_x_bottom, 0, axis_height_bottom, "axis-b")
panel_table <- weave_tables_col(panel_table, axis_mat_y_left, -1, axis_width_left, "axis-l")
panel_table <- weave_tables_col(panel_table, axis_mat_y_right, 0, axis_width_right, "axis-r")
panel_table <- weave_tables_row(panel_table, axis_mat_x_top, -1, axis_height_top, "axis-t", 3)
panel_table <- weave_tables_row(panel_table, axis_mat_x_bottom, 0, axis_height_bottom, "axis-b", 3)
panel_table <- weave_tables_col(panel_table, axis_mat_y_left, -1, axis_width_left, "axis-l", 3)
panel_table <- weave_tables_col(panel_table, axis_mat_y_right, 0, axis_width_right, "axis-r", 3)

strip_padding <- convertUnit(theme$strip.switch.pad.wrap, "cm")
strip_name <- paste0("strip-", substr(params$strip.position, 1, 1))
Expand All @@ -304,7 +304,7 @@ FacetWrap <- ggproto("FacetWrap", Facet,
strip_pad <- axis_height_bottom
}
strip_height <- unit(apply(strip_mat, 1, max_height), "cm")
panel_table <- weave_tables_row(panel_table, strip_mat, placement, strip_height, strip_name)
panel_table <- weave_tables_row(panel_table, strip_mat, placement, strip_height, strip_name, 2, "on")
if (!inside) {
strip_pad[unclass(strip_pad) != 0] <- strip_padding
panel_table <- weave_tables_row(panel_table, row_shift = placement, row_height = strip_pad)
Expand All @@ -320,7 +320,7 @@ FacetWrap <- ggproto("FacetWrap", Facet,
}
strip_pad[unclass(strip_pad) != 0] <- strip_padding
strip_width <- unit(apply(strip_mat, 2, max_width), "cm")
panel_table <- weave_tables_col(panel_table, strip_mat, placement, strip_width, strip_name)
panel_table <- weave_tables_col(panel_table, strip_mat, placement, strip_width, strip_name, 2, "on")
if (!inside) {
strip_pad[unclass(strip_pad) != 0] <- strip_padding
panel_table <- weave_tables_col(panel_table, col_shift = placement, col_width = strip_pad)
Expand Down Expand Up @@ -414,26 +414,26 @@ convertInd <- function(row, col, nrow) {
(col - 1) * nrow + row
}

weave_tables_col <- function(table, table2, col_shift, col_width, name) {
weave_tables_col <- function(table, table2, col_shift, col_width, name, z = 1, clip = "off") {
panel_col <- panel_cols(table)$l
panel_row <- panel_rows(table)$t
for (i in rev(seq_along(panel_col))) {
col_ind <- panel_col[i] + col_shift
table <- gtable_add_cols(table, col_width[i], pos = col_ind)
if (!missing(table2)) {
table <- gtable_add_grob(table, table2[, i], t = panel_row, l = col_ind + 1, clip = 'off', name = paste0(name, '-', seq_along(panel_row), '-', i))
table <- gtable_add_grob(table, table2[, i], t = panel_row, l = col_ind + 1, clip = clip, name = paste0(name, "-", seq_along(panel_row), "-", i), z = z)
}
}
table
}
weave_tables_row <- function(table, table2, row_shift, row_height, name) {
weave_tables_row <- function(table, table2, row_shift, row_height, name, z = 1, clip = "off") {
panel_col <- panel_cols(table)$l
panel_row <- panel_rows(table)$t
for (i in rev(seq_along(panel_row))) {
row_ind <- panel_row[i] + row_shift
table <- gtable_add_rows(table, row_height[i], pos = row_ind)
if (!missing(table2)) {
table <- gtable_add_grob(table, table2[i, ], t = row_ind + 1, l = panel_col, clip = 'off', name = paste0(name, '-', seq_along(panel_col), '-', i))
table <- gtable_add_grob(table, table2[i, ], t = row_ind + 1, l = panel_col, clip = clip, name = paste0(name, "-", seq_along(panel_col), "-", i), z = z)
}
}
table
Expand Down
4 changes: 2 additions & 2 deletions R/labeller.r
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ build_strip <- function(label_df, labeller, theme, horizontal) {
horizontal = horizontal)
heights <- unit(apply(grobs, 2, max_height), "cm")
grobs <- apply(grobs, 1, function(strips) {
gtable_matrix("strip", matrix(strips, ncol = 1), unit(1, "null"), heights, clip = "off")
gtable_matrix("strip", matrix(strips, ncol = 1), unit(1, "null"), heights, clip = "on")
})
list(
top = grobs,
Expand All @@ -505,7 +505,7 @@ build_strip <- function(label_df, labeller, theme, horizontal) {
grobs_right <- grobs_right[, rev(seq_len(ncol(grobs_right))), drop = FALSE]
widths <- unit(apply(grobs_right, 2, max_width), "cm")
grobs_right <- apply(grobs_right, 1, function(strips) {
gtable_matrix("strip", matrix(strips, nrow = 1), widths, unit(1, "null"), clip = "off")
gtable_matrix("strip", matrix(strips, nrow = 1), widths, unit(1, "null"), clip = "on")
})
theme$strip.text.y$angle <- adjust_angle(theme$strip.text.y$angle)
grobs_left <- apply(labels, c(1, 2), ggstrip, theme = theme,
Expand Down