Skip to content

Commit

Permalink
Change theme(title=...) to labs(title=...)
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Jun 27, 2012
1 parent 6661a5b commit 4f5b5f6
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion R/coord-polar.r
Expand Up @@ -38,7 +38,7 @@
#' geom_bar(width = 1) +
#' scale_fill_manual(values = c("red", "yellow")) +
#' coord_polar("y", start=pi / 3) +
#' theme(title = "Pac man")
#' labs(title = "Pac man")
#'
#' # Windrose + doughnut plot
#' movies$rrating <- cut_interval(movies$rating, length = 1)
Expand Down
9 changes: 8 additions & 1 deletion R/labels.r
Expand Up @@ -20,9 +20,11 @@ update_labels <- function(p, labels) {
#' @export
#' @examples
#' p <- qplot(mpg, wt, data = mtcars)
#' p + labs(title = "New plot title")
#' p + labs(x = "New x label")
#' p + xlab("New x label")
#' p + ylab("New y label")
#' p + ggtitle("New plot title")
#'
#' # This should work indepdendently of other functions that modify the
#' # the scale names
Expand All @@ -34,7 +36,7 @@ update_labels <- function(p, labels) {
#' p + labs(colour = "Cylinders")
#'
#' # Can also pass in a list, if that is more convenient
#' p + labs(list(x = "X", y = "Y"))
#' p + labs(list(title = "Title", x = "X", y = "Y"))
labs <- function(...) {
args <- list(...)
if (is.list(args[[1]])) args <- args[[1]]
Expand All @@ -52,6 +54,11 @@ xlab <- function(label) {
ylab <- function(label) {
labs(y = label)
}
#' @rdname labs
#' @export
ggtitle <- function(label) {
labs(title = label)
}

# Convert aesthetic mapping into text labels
make_labels <- function(mapping) {
Expand Down
4 changes: 2 additions & 2 deletions R/plot-render.r
Expand Up @@ -131,9 +131,9 @@ ggplot_gtable <- function(data) {
}

# Title
title <- element_render(theme, "plot.title", plot$options$title)
title <- element_render(theme, "plot.title", plot$options$label$title)
title_height <- grobHeight(title) +
if (is.null(plot$options$title)) unit(0, "lines") else unit(0.5, "lines")
if (is.null(plot$options$label$title)) unit(0, "lines") else unit(0.5, "lines")

pans <- subset(plot_table$layout, grepl("^panel", name))

Expand Down
3 changes: 1 addition & 2 deletions R/theme-elements.r
Expand Up @@ -295,6 +295,5 @@ el_def <- function(class = NULL, inherits = NULL, description = NULL) {

plot.background = el_def("element_rect", "rect"),
plot.title = el_def("element_text", "text.title"),
plot.margin = el_def("unit"),
title = el_def("character")
plot.margin = el_def("unit")
)
16 changes: 12 additions & 4 deletions R/theme.r
Expand Up @@ -111,10 +111,8 @@ print.theme <- function(x, ...) str(x)
#' # Or
#' p + labs(x = "Vehicle Weight", y = "Miles per Gallon")
#'
#' # Add a title to the plot
#' p + theme(title = "Vehicle Weight-Gas Mileage Relationship")
#' # Change title appearance
#' p <- p + theme(title = "Vehicle Weight-Gas Mileage Relationship")
#' p <- p + labs(title = "Vehicle Weight-Gas Mileage Relationship")
#' p + theme(plot.title = element_text(size = 20))
#' p + theme(plot.title = element_text(size = 20, colour = "Blue"))
#'
Expand Down Expand Up @@ -182,7 +180,17 @@ print.theme <- function(x, ...) str(x)
#' k + theme(panel.margin = unit(0, "lines"))
#' }
theme <- function(...) {
structure(list(...), class="theme")
# Add check for deprecated elements
extra <- NULL
elements <- list(...)
if (!is.null(elements[["title"]])) {
# This is kind of a hack, but fortunately it will be removed in future versions
warning('Setting the plot title with theme(title="...") is deprecated.',
' Use labs(title="...") or ggtitle("...") instead.')
elements$labels <- labs(title = elements[["title"]])
}

structure(elements, class="theme")
}


Expand Down
2 changes: 1 addition & 1 deletion R/translate-qplot-base.r
Expand Up @@ -140,6 +140,6 @@
#' # by box() can be controlled in a similar way by the panel.background and
#' # plot.background theme elements. Instead of using title(), the plot title is
#' # set with the title option. See ?theme for more theme elements.
#' last_plot() + theme(title = "My Plot Title")
#' last_plot() + labs(title = "My Plot Title")
#' }
NULL
2 changes: 1 addition & 1 deletion R/translate-qplot-ggplot.r
Expand Up @@ -78,5 +78,5 @@
#' # qplot() recognises the same options as plot does, and converts them to their
#' # ggplot2 equivalents. See ?theme for more on ggplot options
#' # qplot(x, y, data = data, main="title", asp = 1)
#' # ggplot(data, aes(x, y)) + geom_point() + theme(title = "title", aspect.ratio = 1)
#' # ggplot(data, aes(x, y)) + geom_point() + labs(title = "title") + theme(aspect.ratio = 1)
NULL
3 changes: 2 additions & 1 deletion visual_test/guide-position.r
@@ -1,6 +1,7 @@
vcontext("guide-position")

p1 <- ggplot(mtcars, aes(mpg, disp, colour=cyl)) + geom_point() + theme(title = "title of plot", axis.text.x = element_text(angle = 90)) +
p1 <- ggplot(mtcars, aes(mpg, disp, colour=cyl)) + geom_point() + labs(title = "title of plot") +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_continuous(breaks = mean(mtcars$mpg), labels = "very very long long axis label") +
scale_y_continuous(breaks = mean(mtcars$disp), labels = "very very long long axis label")

Expand Down

0 comments on commit 4f5b5f6

Please sign in to comment.