Skip to content
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ visualizations.
- Fixed margin spacing for free facets with different axis scales. The dynamic
margin adjustment now computes tick labels per-facet and sizes the margin to
accommodate the widest labels. (#579 @grantmcdermott)
- Fixed several minor issues related to facet title strips and spacing: Strips
and spacing now scale correctly with `facet.cex`, multiline facet titles
inter-panel gaps remain fixed regardless of strip height, and strip text is
now vertically centered within the background rect. (#586 @grantmcdermott)

## v0.6.1

Expand Down
62 changes: 28 additions & 34 deletions R/facet.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ draw_facet_window = function(

ooma = par("oma")

# Bump top margin down for facet titles
fmar[3] = fmar[3] + 1
# Bump top margin for facet strip. Use facet_text (not / cex_fct_adj)
# because nmar = (fmar + 0.1) / cex_fct_adj already divides — using
# facet_text directly keeps the inter-panel gap constant as newlines grow.
fmar[3] = fmar[3] + facet_text
if (isTRUE(attr(facet, "facet_grid"))) {
fmar[3] = max(0, fmar[3] - 1)
fmar[3] = max(0, fmar[3] - facet_text)
# Indent for RHS facet_grid title strip if "right!" legend
if (has_legend && ooma[4] > 0) ooma[4] = ooma[4] + 1
}
fmar[3] = fmar[3] + facet_newlines * facet_text / cex_fct_adj
fmar[3] = fmar[3] + facet_newlines * facet_text

omar = par("mar")

Expand All @@ -124,18 +126,10 @@ draw_facet_window = function(
# reset by the before.plot.new hook.
side.sub = get_tpar("side.sub", tpar_list = tpars, default = 3)
omar = dynmar_computed
# Under facets, main/sub sit ABOVE the top facet strip. Bump the
# outer top margin by the strip height so sub doesn't collide with
# the strip. fmar[3] already captures facet_newlines and the
# facet_grid adjustment; add back the 0.5 line that was stripped
# when frame.plot is FALSE (that reduction is meant to tighten
# inter-panel gaps, not the top strip).
strip_bump = fmar[3]
if (isFALSE(frame.plot) && !isTRUE(facet.args[["free"]])) {
strip_bump = strip_bump + 0.5
}
omar[3] = omar[3] + strip_bump

omar[3] = dynmar_computed[3] + (1 + facet_newlines + 0.1) * facet_text
# Ensure fmar[3] doesn't exceed omar[3] - 0.1, which would make
# noma[3] negative and get clamped to 0, creating excess top space.
if (fmar[3] + 0.1 > omar[3]) fmar[3] = omar[3] - 0.1
if (par("las") %in% 1:2) {
# extra whitespace bump on the y axis
## overrides for ridge and some types that use integer spacing with (named) axis labels ## FXIME
Expand Down Expand Up @@ -415,13 +409,13 @@ draw_facet_window = function(
if (isTRUE(attr(facet, "facet_grid"))) {
## top facet strips
if (ii %in% 1:nfacet_cols) {
line_height_lines = (facet_title_lines + .1) * facet_text / cex_fct_adj
if (isTRUE(facet_rect)) {
line_height = (facet_title_lines + .1) * facet_text / cex_fct_adj
if (ylog) {
line_height = grconvertY(line_height, from = "lines", to = "user") / grconvertY(0, from = "lines", to = "user")
line_height = grconvertY(line_height_lines, from = "lines", to = "user") / grconvertY(0, from = "lines", to = "user")
rect_height = corners[4] * line_height
} else {
line_height = grconvertY(line_height, from = "lines", to = "user") - grconvertY(0, from = "lines", to = "user")
line_height = grconvertY(line_height_lines, from = "lines", to = "user") - grconvertY(0, from = "lines", to = "user")
rect_height = corners[4] + line_height
}
rect(
Expand All @@ -432,17 +426,17 @@ draw_facet_window = function(
}
xpos = if (xlog) 10^(mean(log10(corners[1:2]))) else mean(corners[1:2])
if (ylog) {
ypos = grconvertY(0.4, from = "lines", to = "user") / grconvertY(0, from = "lines", to = "user")
ypos = grconvertY(line_height_lines / 2, from = "lines", to = "user") / grconvertY(0, from = "lines", to = "user")
ypos = corners[4] * ypos
} else {
ypos = grconvertY(0.4, from = "lines", to = "user") - grconvertY(0, from = "lines", to = "user")
ypos = grconvertY(line_height_lines / 2, from = "lines", to = "user") - grconvertY(0, from = "lines", to = "user")
ypos = corners[4] + ypos
}
text(
x = xpos,
y = ypos,
labels = sub("^(.*?)~.*", "\\1", facets[[ii]]),
adj = c(0.5, 0),
adj = c(0.5, 0.5),
cex = facet_text / cex_fct_adj,
col = facet_col,
font = facet_font,
Expand All @@ -451,13 +445,13 @@ draw_facet_window = function(
}
## right facet strips
if (ii %% nfacet_cols == 0 || ii == nfacets) {
line_height_lines = (facet_title_lines + .1) * facet_text / cex_fct_adj
if (isTRUE(facet_rect)) {
line_height = (facet_title_lines + .1) * facet_text / cex_fct_adj
if (xlog) {
line_height = grconvertX(line_height, from = "lines", to = "user") / grconvertX(0, from = "lines", to = "user")
line_height = grconvertX(line_height_lines, from = "lines", to = "user") / grconvertX(0, from = "lines", to = "user")
rect_width = corners[2] * line_height
} else {
line_height = grconvertX(line_height, from = "lines", to = "user") - grconvertX(0, from = "lines", to = "user")
line_height = grconvertX(line_height_lines, from = "lines", to = "user") - grconvertX(0, from = "lines", to = "user")
rect_width = corners[2] + line_height
}
rect(
Expand All @@ -467,10 +461,10 @@ draw_facet_window = function(
)
}
if (xlog) {
xpos = grconvertX(0.4, from = "lines", to = "user") / grconvertX(0, from = "lines", to = "user")
xpos = grconvertX(line_height_lines / 2, from = "lines", to = "user") / grconvertX(0, from = "lines", to = "user")
xpos = corners[2] * xpos
} else {
xpos = grconvertX(0.4, from = "lines", to = "user") - grconvertX(0, from = "lines", to = "user")
xpos = grconvertX(line_height_lines / 2, from = "lines", to = "user") - grconvertX(0, from = "lines", to = "user")
xpos = corners[2] + xpos
}
ypos = if (ylog) 10^(mean(log10(corners[3:4]))) else mean(corners[3:4])
Expand All @@ -479,21 +473,21 @@ draw_facet_window = function(
y = ypos,
labels = sub("^.*?~(.*)", "\\1", facets[[ii]]),
srt = 270,
adj = c(0.5, 0),
adj = c(0.5, 0.5),
cex = facet_text / cex_fct_adj,
col = facet_col,
font = facet_font,
xpd = NA
)
}
} else {
line_height_lines = (facet_title_lines + .1) * facet_text / cex_fct_adj
if (isTRUE(facet_rect)) {
line_height = (facet_title_lines + .1) * facet_text / cex_fct_adj
if (ylog) {
line_height = grconvertY(line_height, from = "lines", to = "user") / grconvertY(0, from = "lines", to = "user")
line_height = grconvertY(line_height_lines, from = "lines", to = "user") / grconvertY(0, from = "lines", to = "user")
rect_height = corners[4] * line_height
} else {
line_height = grconvertY(line_height, from = "lines", to = "user") - grconvertY(0, from = "lines", to = "user")
line_height = grconvertY(line_height_lines, from = "lines", to = "user") - grconvertY(0, from = "lines", to = "user")
rect_height = corners[4] + line_height
}
rect(
Expand All @@ -504,17 +498,17 @@ draw_facet_window = function(
}
xpos = if (xlog) 10^(mean(log10(corners[1:2]))) else mean(corners[1:2])
if (ylog) {
ypos = grconvertY(0.4, from = "lines", to = "user") / grconvertY(0, from = "lines", to = "user")
ypos = grconvertY(line_height_lines / 2, from = "lines", to = "user") / grconvertY(0, from = "lines", to = "user")
ypos = corners[4] * ypos
} else {
ypos = grconvertY(0.4, from = "lines", to = "user") - grconvertY(0, from = "lines", to = "user")
ypos = grconvertY(line_height_lines / 2, from = "lines", to = "user") - grconvertY(0, from = "lines", to = "user")
ypos = corners[4] + ypos
}
text(
x = xpos,
y = ypos,
labels = paste(facets[[ii]]),
adj = c(0.5, 0),
adj = c(0.5, 0.5),
cex = facet_text / cex_fct_adj,
col = facet_col,
font = facet_font,
Expand Down
2 changes: 1 addition & 1 deletion R/tinyplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ tinyplot.default = function(
# if (length(facet_newlines)==0) facet_newlines = 0
# omar[3] = omar[3] + max(facet_newlines)
facet_newlines = ifelse(length(facet_newlines) == 0, 0, max(facet_newlines))
omar[3] = omar[3] + facet_newlines * facet_text / cex_fct_adj
omar[3] = omar[3] + facet_newlines * facet_text
# apply the changes
par(mar = omar)
}
Expand Down
4 changes: 2 additions & 2 deletions inst/tinytest/_tinysnapshot/barplot_aggregation.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions inst/tinytest/_tinysnapshot/barplot_facet.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions inst/tinytest/_tinysnapshot/barplot_facet_free.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions inst/tinytest/_tinysnapshot/barplot_flip_fancy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading