Skip to content

Let scales modify title #1868

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 5 commits into from
Oct 26, 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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,4 @@ Collate:
'zxx.r'
'zzz.r'
VignetteBuilder: knitr
RoxygenNote: 5.0.1.9000
RoxygenNote: 5.0.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export("%+%")
export("%+replace%")
export(.pt)
export(.stroke)
export(AxisSecondary)
export(Coord)
export(CoordCartesian)
export(CoordFixed)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ There were a number of tweaks to the theme elements that control legends:
aesthetics they accept: `xmin_final`, `xmax_final`, `xlower`,
`xmiddle` and `xupper` are now valid `x` aesthetics.

* `Scale` extensions can now override the `make_title` and `make_sec_title`
methods to let the scale modify the axis/legend titles.

# ggplot2 2.1.0

## New features
Expand Down
8 changes: 8 additions & 0 deletions R/axis-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ is.derived <- function(x) {
inherits(x, "derived")
}
#' @importFrom lazyeval f_eval
#'
#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
AxisSecondary <- ggproto("AxisSecondary", NULL,
trans = NULL,
axis = NULL,
Expand Down Expand Up @@ -149,5 +154,8 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
)
scale$train(range)
scale
},
make_title = function(title) {
title
}
)
2 changes: 1 addition & 1 deletion R/guides-.r
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ guides_train <- function(scales, theme, guides, labels) {
if (guide$available_aes != "any" && !scale$aesthetics %in% guide$available_aes)
stop("Guide '", guide$name, "' cannot be used for '", scale$aesthetics, "'.")

guide$title <- guide$title %|W|% scale$name %|W|% labels[[output]]
guide$title <- scale$make_title(guide$title %|W|% scale$name %|W|% labels[[output]])

# direction of this grob
guide$direction <- guide$direction %||% theme$legend.direction
Expand Down
4 changes: 4 additions & 0 deletions R/layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,25 @@ Layout <- ggproto("Layout", NULL,

xlabel = function(self, labels) {
primary <- self$panel_scales$x[[1]]$name %|W|% labels$x
primary <- self$panel_scales$x[[1]]$make_title(primary)
secondary <- if (is.null(self$panel_scales$x[[1]]$secondary.axis)) {
waiver()
} else {
self$panel_scales$x[[1]]$sec_name()
} %|W|% labels$sec.x
secondary <- self$panel_scales$x[[1]]$make_sec_title(secondary)
list(primary = primary, secondary = secondary)[self$panel_scales$x[[1]]$axis_order()]
},

ylabel = function(self, labels) {
primary <- self$panel_scales$y[[1]]$name %|W|% labels$y
primary <- self$panel_scales$y[[1]]$make_title(primary)
secondary <- if (is.null(self$panel_scales$y[[1]]$secondary.axis)) {
waiver()
} else {
self$panel_scales$y[[1]]$sec_name()
} %|W|% labels$sec.y
secondary <- self$panel_scales$y[[1]]$make_sec_title(secondary)
list(primary = primary, secondary = secondary)[self$panel_scales$y[[1]]$axis_order()]
},

Expand Down
8 changes: 8 additions & 0 deletions R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ Scale <- ggproto("Scale", NULL,
ord <- rev(ord)
}
ord
},

# Here to make it possible for scales to modify the default titles
make_title = function(title) {
title
},
make_sec_title = function(title) {
title
}
)

Expand Down
7 changes: 7 additions & 0 deletions R/scale-continuous.r
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ ScaleContinuousPosition <- ggproto("ScaleContinuousPosition", ScaleContinuous,
} else {
self$secondary.axis$name
}
},
make_sec_title = function(self, title) {
if (!is.waive(self$secondary.axis)) {
self$secondary.axis$make_title(title)
} else {
ggproto_parent(ScaleContinuous, self)$make_sec_title(title)
}
}
)

Expand Down
2 changes: 1 addition & 1 deletion man/aes_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/aes_colour_fill_alpha.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/aes_linetype_size_shape.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/aes_position.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/coord_fixed.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/cut_interval.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/draw_key.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/element.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/fortify-multcomp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/fortify.sp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/geom_bin2d.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/geom_density_2d.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/geom_histogram.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/geom_linerange.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/geom_path.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/geom_ribbon.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/geom_segment.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/ggplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading