34
34
# ' @param scale Multiplicative scaling factor.
35
35
# ' @param width,height Plot size in units expressed by the `units` argument.
36
36
# ' If not supplied, uses the size of the current graphics device.
37
+ # ' Alternatively, these can be set to `derived()` in order to use innate
38
+ # ' plot dimensions for output. This is useful when the
39
+ # ' `theme(panel.widths, panel.heights)` options are set to absolute units.
37
40
# ' @param units One of the following units in which the `width` and `height`
38
41
# ' arguments are expressed: `"in"`, `"cm"`, `"mm"` or `"px"`.
39
42
# ' @param dpi Plot resolution. Also accepts a string input: "retina" (320),
@@ -200,6 +203,8 @@ plot_dim <- function(width = NA, height = NA, scale = 1, units = "in",
200
203
from_inches <- function (x ) x * c(`in` = 1 , cm = 2.54 , mm = 2.54 * 10 , px = dpi )[units ]
201
204
202
205
if (is.derived(width ) || is.derived(height )) {
206
+ # To size from plot if width or height are derived
207
+ # TODO: use gtable::as.gtable when implemented
203
208
if (is.ggplot(plot )) {
204
209
plot <- ggplotGrob(plot )
205
210
}
@@ -216,6 +221,7 @@ plot_dim <- function(width = NA, height = NA, scale = 1, units = "in",
216
221
217
222
if (is.unit(width )) {
218
223
if (has_null_unit(width )) {
224
+ # When plot has no absolute dimensions, fall back to device size
219
225
width <- NA
220
226
} else {
221
227
width <- from_inches(convertWidth(width , " in" , valueOnly = TRUE ))
@@ -224,6 +230,7 @@ plot_dim <- function(width = NA, height = NA, scale = 1, units = "in",
224
230
225
231
if (is.unit(height )) {
226
232
if (has_null_unit(height )) {
233
+ # When plot has no absolute dimensions, fall back to device size
227
234
height <- NA
228
235
} else {
229
236
height <- from_inches(convertHeight(height , " in" , valueOnly = TRUE ))
0 commit comments