Skip to content

Commit 1ce6014

Browse files
committed
added roxygen bits and adhered to coding style standards
1 parent 44142f7 commit 1ce6014

File tree

7 files changed

+46
-15
lines changed

7 files changed

+46
-15
lines changed

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
aesthetics they accept: `xmin_final`, `xmax_final`, `xlower`,
55
`xmiddle` and `xupper` are now valid `x` aesthetics.
66

7+
* `ggtitle` and `labs` now take a `subtitle` parameter which makes it
8+
possible to add a subtitle below the main plot title.
9+
10+
* the main plot title and subtitle are left-justified (`hjust = 0`) by
11+
default
12+
713
# ggplot2 2.1.0
814

915
## New features

R/labels.r

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ update_labels <- function(p, labels) {
2828
#' p + ylab("New y label")
2929
#' p + ggtitle("New plot title")
3030
#'
31+
#' # Can add a subtitle to plots with either of the following
32+
#' p + ggtitle("New plot title", subtitle = "A subtitle")
33+
#' p + labs(title = "New plot title", subtitle = "A subtitle")
34+
#'
3135
#' # This should work independently of other functions that modify the
3236
#' # the scale names
3337
#' p + ylab("New y label") + ylim(2, 4)
@@ -38,7 +42,7 @@ update_labels <- function(p, labels) {
3842
#' p + labs(colour = "Cylinders")
3943
#'
4044
#' # Can also pass in a list, if that is more convenient
41-
#' p + labs(list(title = "Title", x = "X", y = "Y"))
45+
#' p + labs(list(title = "Title", subtitle = "Subtitle", x = "X", y = "Y"))
4246
labs <- function(...) {
4347
args <- list(...)
4448
if (is.list(args[[1]])) args <- args[[1]]
@@ -58,14 +62,12 @@ ylab <- function(label) {
5862
}
5963
#' @rdname labs
6064
#' @export
61-
ggtitle <- function(label) {
62-
labs(title = label)
63-
}
64-
65-
#' @rdname labs
66-
#' @export
67-
ggsubtitle <- function(label) {
68-
labs(subtitle = label)
65+
ggtitle <- function(label, subtitle) {
66+
if (!missing(subtitle)) {
67+
labs(title = label, subtitle = subtitle)
68+
} else {
69+
labs(title = label)
70+
}
6971
}
7072

7173
# Convert aesthetic mapping into text labels

R/theme-defaults.r

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,12 @@ theme_grey <- function(base_size = 11, base_family = "") {
126126
plot.background = element_rect(colour = "white"),
127127
plot.title = element_text(
128128
size = rel(1.2),
129+
hjust = 0,
129130
margin = margin(b = half_line * 1.2)
130131
),
131132
plot.subtitle = element_text(
132133
size = rel(0.9),
134+
hjust = 0,
133135
margin = margin(b = half_line * 0.9)
134136
),
135137
plot.margin = margin(half_line, half_line, half_line, half_line),

R/theme-elements.r

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
224224
rect = el_def("element_rect"),
225225
text = el_def("element_text"),
226226
title = el_def("element_text", "text"),
227-
subtitle = el_def("element_text", "text"),
228227
axis.line = el_def("element_line", "line"),
229228
axis.text = el_def("element_text", "text"),
230229
axis.title = el_def("element_text", "title"),
@@ -279,7 +278,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
279278

280279
plot.background = el_def("element_rect", "rect"),
281280
plot.title = el_def("element_text", "title"),
282-
plot.subtitle = el_def("element_text", "subtitle"),
281+
plot.subtitle = el_def("element_text", "title"),
283282
plot.margin = el_def("margin"),
284283

285284
aspect.ratio = el_def("character")

R/theme.r

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ print.theme <- function(x, ...) utils::str(x)
187187
#' plot.background \tab background of the entire plot
188188
#' (\code{element_rect}; inherits from \code{rect}) \cr
189189
#' plot.title \tab plot title (text appearance)
190-
#' (\code{element_text}; inherits from \code{title}) \cr
190+
#' (\code{element_text}; inherits from \code{title})
191+
#' left-aligned by default\cr
192+
#' plot.subtitle \tab plot title (text appearance)
193+
#' (\code{element_text}; inherits from \code{title})
194+
#' left-aligned by default\cr
191195
#' plot.margin \tab margin around entire plot
192196
#' (\code{unit} with the sizes of the top, right, bottom, and
193197
#' left margins) \cr
@@ -254,6 +258,11 @@ print.theme <- function(x, ...) utils::str(x)
254258
#' p + theme(plot.title = element_text(size = rel(2)))
255259
#' p + theme(plot.title = element_text(size = rel(2), colour = "blue"))
256260
#'
261+
#' # Add a subtitle and adjust bottom margin
262+
#' p + labs(title = "Vehicle Weight-Gas Mileage Relationship",
263+
#' subtitle = "You need to wrap long subtitleson manually") +
264+
#' theme(plot.subtitle = element_text(margin = margin(b = 20)))
265+
#'
257266
#' # Changing plot look with themes
258267
#' DF <- data.frame(x = rnorm(400))
259268
#' m <- ggplot(DF, aes(x = x)) +

man/labs.Rd

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/theme.Rd

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)