Skip to content

Commit cd11888

Browse files
committed
Fix aspect ratio in facet_wrap. First pass at improved coord_equal - modulo panel size problem
1 parent 2e15938 commit cd11888

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ ggplot2 0.8.5 (2009-XX-XX) ----------------------------------------
3737
* legends will only merge if both the title and all labels are the same.
3838
(Fixes #16)
3939
* Fix for bug #19: Legend with three constraints doesn't work
40-
* Legend: correctly calculate label widths for all font sizes. (Fixes #38)
40+
* Legend: correctly calculate label widths for all font sizes. (Fixes #38)
41+
* facet_wrap: now sets aspect ratio correctly
42+
* coord_equal: when ratio = NULL (the default), will adjust the aspect ratio
43+
of the plot, rather than trying to extend the shortest scale.

R/coord-.r

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ Coord <- proto(TopLevel, expr={
3434
)
3535
}
3636

37+
compute_aspect <- function(., ranges) {
38+
NULL
39+
}
40+
3741
labels <- function(., scales) {
3842
scales
3943
}

R/coord-cartesian-equal.r

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CoordEqual <- proto(CoordCartesian, {
22

3-
new <- function(., ratio=1) {
4-
list(.$proto(ratio = ratio), opts(aspect.ratio = ratio))
3+
new <- function(., ratio = NULL) {
4+
.$proto(ratio = ratio)
55
}
66

77
compute_ranges <- function(., scales) {
@@ -13,6 +13,12 @@ CoordEqual <- proto(CoordCartesian, {
1313

1414
.super$compute_ranges(., scales)
1515
}
16+
17+
compute_aspect <- function(., ranges) {
18+
if (!is.null(.$ratio)) return(.$ratio)
19+
20+
diff(ranges$y.range) / diff(ranges$x.range)
21+
}
1622

1723
# Documentation -----------------------------------------------
1824

@@ -23,8 +29,12 @@ CoordEqual <- proto(CoordCartesian, {
2329
details <- "<p>An equal scale coordinate system plays a similar role to ?eqscplot in MASS, but it works for all types of graphics, not just scatterplots.</p>\n<p>This coordinate system has one parameter, <code>ratio</code>, which specifies the ratio between the x and y scales. An aspect ratio of two means that the plot will be twice as high as wide. An aspection ratio of 1/2 means that the plot will be twice as wide as high. By default, the aspect.ratio of the plot will also be set to this value.</p>\n"
2430

2531
examples <- function(.) {
26-
# coord_equal ensures that the ranges of axes are equal to the
27-
# specified ratio (1 by default, indicating equal ranges).
32+
# ratio = NULL, the default, will modify the aspect ratio of the plot
33+
# to
34+
qplot(mpg, wt, data = mtcars) + coord_equal()
35+
36+
# when ratio = a specific number, ensures that the ranges of axes are
37+
# equal to the specified ratio by expanding the smallest axis
2838

2939
qplot(mpg, wt, data = mtcars) + coord_equal(ratio = 1)
3040
qplot(mpg, wt, data = mtcars) + coord_equal(ratio = 5)
@@ -44,7 +54,9 @@ CoordEqual <- proto(CoordCartesian, {
4454
# @arguments y limits
4555
# @arguments desired ratio between x and y ranges
4656
# @keywords internal
47-
equal_ranges <- function(xlim, ylim, ratio = 1) {
57+
equal_ranges <- function(xlim, ylim, ratio = NULL) {
58+
if (is.null(ratio)) return(list(x = xlim, y = ylim))
59+
4860
xr <- diff(xlim)
4961
yr <- diff(ylim)
5062

R/facet-grid-.r

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,18 @@ FacetGrid <- proto(Facet, {
4343

4444
# Create grobs for each component of the panel guides
4545
add_guides <- function(., data, panels_grob, coord, theme) {
46+
4647
aspect_ratio <- theme$aspect.ratio
48+
49+
# If user hasn't set aspect ratio, and we have fixed scales, then
50+
# ask the coordinate system if it wants to specify one
51+
if (is.null(aspect_ratio) && !.$free$x && !.$free$y) {
52+
xscale <- .$scales$x[[1]]
53+
yscale <- .$scales$y[[1]]
54+
ranges <- coord$compute_ranges(list(x = xscale, y = yscale))
55+
aspect_ratio <- coord$compute_aspect(ranges)
56+
}
57+
4758
if (is.null(aspect_ratio)) {
4859
aspect_ratio <- 1
4960
respect <- FALSE

R/facet-wrap.r

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,25 @@ FacetWrap <- proto(Facet, {
4444

4545
# Create grobs for each component of the panel guides
4646
add_guides <- function(., data, panels_grob, coord, theme) {
47+
4748
aspect_ratio <- theme$aspect.ratio
48-
if (is.null(aspect_ratio)) aspect_ratio <- 1
4949

50+
# If user hasn't set aspect ratio, and we have fixed scales, then
51+
# ask the coordinate system if it wants to specify one
52+
if (is.null(aspect_ratio) && !.$free$x && !.$free$y) {
53+
xscale <- .$scales$x[[1]]
54+
yscale <- .$scales$y[[1]]
55+
ranges <- coord$compute_ranges(list(x = xscale, y = yscale))
56+
aspect_ratio <- coord$compute_aspect(ranges)
57+
}
58+
59+
if (is.null(aspect_ratio)) {
60+
aspect_ratio <- 1
61+
respect <- FALSE
62+
} else {
63+
respect <- TRUE
64+
}
65+
5066
n <- length(.$scales$x)
5167

5268
axes_h <- matrix(list(), nrow = 1, ncol = n)
@@ -88,7 +104,7 @@ FacetWrap <- proto(Facet, {
88104
panelsGrid <- grobGrid(
89105
"panel", panels, nrow = nrow, ncol = ncol,
90106
heights = 1 * aspect_ratio, widths = 1,
91-
as.table = .$as.table
107+
as.table = .$as.table, respect = respect
92108
)
93109

94110
strips <- .$labels_default(.$facet_levels, theme)

0 commit comments

Comments
 (0)