Skip to content

Commit

Permalink
Style code used in examples (#4098)
Browse files Browse the repository at this point in the history
Fixes #4092
  • Loading branch information
rjake authored Jul 1, 2020
1 parent cb0cfd2 commit 3aa2937
Show file tree
Hide file tree
Showing 59 changed files with 538 additions and 220 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Fixed a bug in `labeller()` so that `.default` is passed to `as_labeller()`
when labellers are specified by naming faceting variables. (@waltersom, #4031)

* Updated style for example code (@rjake, #4092)

# ggplot2 3.3.2
This is a small release focusing on fixing regressions introduced in 3.3.1.
Expand Down
7 changes: 5 additions & 2 deletions R/aes-colour-fill-alpha.r
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@
#' p <- ggplot(economics, aes(x = date, y = unemploy)) + geom_line()
#' p
#' yrng <- range(economics$unemploy)
#' p <- p + geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party),
#' ymin = yrng[1], ymax = yrng[2], data = presidential)
#' p <- p +
#' geom_rect(
#' aes(NULL, NULL, xmin = start, xmax = end, fill = party),
#' ymin = yrng[1], ymax = yrng[2], data = presidential
#' )
#' p
#' p + scale_fill_manual(values = alpha(c("blue", "red"), .3))
#' }
Expand Down
40 changes: 26 additions & 14 deletions R/axis-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,39 @@
#'
#' # Secondary axes work for date and datetime scales too:
#' df <- data.frame(
#' dx = seq(as.POSIXct("2012-02-29 12:00:00",
#' tz = "UTC",
#' format = "%Y-%m-%d %H:%M:%S"
#' ),
#' length.out = 10, by = "4 hour"
#' dx = seq(
#' as.POSIXct("2012-02-29 12:00:00", tz = "UTC"),
#' length.out = 10,
#' by = "4 hour"
#' ),
#' price = seq(20, 200000, length.out = 10)
#' )
#'
#' # This may useful for labelling different time scales in the same plot
#' ggplot(df, aes(x = dx, y = price)) + geom_line() +
#' scale_x_datetime("Date", date_labels = "%b %d",
#' date_breaks = "6 hour",
#' sec.axis = dup_axis(name = "Time of Day",
#' labels = scales::time_format("%I %p")))
#' ggplot(df, aes(x = dx, y = price)) +
#' geom_line() +
#' scale_x_datetime(
#' "Date",
#' date_labels = "%b %d",
#' date_breaks = "6 hour",
#' sec.axis = dup_axis(
#' name = "Time of Day",
#' labels = scales::time_format("%I %p")
#' )
#' )
#'
#' # or to transform axes for different timezones
#' ggplot(df, aes(x = dx, y = price)) + geom_line() +
#' scale_x_datetime("GMT", date_labels = "%b %d %I %p",
#' sec.axis = sec_axis(~ . + 8 * 3600, name = "GMT+8",
#' labels = scales::time_format("%b %d %I %p")))
#' ggplot(df, aes(x = dx, y = price)) +
#' geom_line() +
#' scale_x_datetime("
#' GMT",
#' date_labels = "%b %d %I %p",
#' sec.axis = sec_axis(
#' ~ . + 8 * 3600,
#' name = "GMT+8",
#' labels = scales::time_format("%b %d %I %p")
#' )
#' )
#'
#' @export
sec_axis <- function(trans = NULL, name = waiver(), breaks = waiver(), labels = waiver(),
Expand Down
17 changes: 12 additions & 5 deletions R/geom-dotplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@
#' @references Wilkinson, L. (1999) Dot plots. The American Statistician,
#' 53(3), 276-281.
#' @examples
#' ggplot(mtcars, aes(x = mpg)) + geom_dotplot()
#' ggplot(mtcars, aes(x = mpg)) + geom_dotplot(binwidth = 1.5)
#' ggplot(mtcars, aes(x = mpg)) +
#' geom_dotplot()
#'
#' ggplot(mtcars, aes(x = mpg)) +
#' geom_dotplot(binwidth = 1.5)
#'
#' # Use fixed-width bins
#' ggplot(mtcars, aes(x = mpg)) +
Expand All @@ -70,6 +73,7 @@
#' # Some other stacking methods
#' ggplot(mtcars, aes(x = mpg)) +
#' geom_dotplot(binwidth = 1.5, stackdir = "center")
#'
#' ggplot(mtcars, aes(x = mpg)) +
#' geom_dotplot(binwidth = 1.5, stackdir = "centerwhole")
#'
Expand All @@ -78,13 +82,16 @@
#' scale_y_continuous(NULL, breaks = NULL)
#'
#' # Overlap dots vertically
#' ggplot(mtcars, aes(x = mpg)) + geom_dotplot(binwidth = 1.5, stackratio = .7)
#' ggplot(mtcars, aes(x = mpg)) +
#' geom_dotplot(binwidth = 1.5, stackratio = .7)
#'
#' # Expand dot diameter
#' ggplot(mtcars, aes(x = mpg)) + geom_dotplot(binwidth = 1.5, dotsize = 1.25)
#' ggplot(mtcars, aes(x = mpg)) +
#' geom_dotplot(binwidth = 1.5, dotsize = 1.25)
#'
#' # Change dot fill colour, stroke width
#' ggplot(mtcars, aes(x = mpg)) + geom_dotplot(binwidth = 1.5, fill = "white", stroke = 2)
#' ggplot(mtcars, aes(x = mpg)) +
#' geom_dotplot(binwidth = 1.5, fill = "white", stroke = 2)
#'
#' \donttest{
#' # Examples with stacking along y axis instead of x
Expand Down
7 changes: 5 additions & 2 deletions R/geom-errorbarh.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
#' # Define the top and bottom of the errorbars
#'
#' p <- ggplot(df, aes(resp, trt, colour = group))
#' p + geom_point() +
#' p +
#' geom_point() +
#' geom_errorbarh(aes(xmax = resp + se, xmin = resp - se))
#' p + geom_point() +
#'
#' p +
#' geom_point() +
#' geom_errorbarh(aes(xmax = resp + se, xmin = resp - se, height = .2))
geom_errorbarh <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
Expand Down
11 changes: 10 additions & 1 deletion R/geom-function.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
#' geom_function(fun = dnorm, colour = "red")
#'
#' # To plot functions without data, specify range of x-axis
#' base <- ggplot() + xlim(-5, 5)
#' base <-
#' ggplot() +
#' xlim(-5, 5)
#'
#' base + geom_function(fun = dnorm)
#'
#' base + geom_function(fun = dnorm, args = list(mean = 2, sd = .5))
#'
#' # The underlying mechanics evaluate the function at discrete points
#' # and connect the points with lines
#' base + stat_function(fun = dnorm, geom = "point")
#'
#' base + stat_function(fun = dnorm, geom = "point", n = 20)
#'
#' base + geom_function(fun = dnorm, n = 20)
#'
#' # Two functions on the same plot
Expand All @@ -35,11 +41,14 @@
#'
#' # Using a custom anonymous function
#' base + geom_function(fun = function(x) 0.5*exp(-abs(x)))
#'
#' base + geom_function(fun = ~ 0.5*exp(-abs(.x)))
#'
#' # Using a custom named function
#' f <- function(x) 0.5*exp(-abs(x))
#'
#' base + geom_function(fun = f)
#'
#' @export
geom_function <- function(mapping = NULL, data = NULL, stat = "function",
position = "identity", ..., na.rm = FALSE,
Expand Down
24 changes: 18 additions & 6 deletions R/geom-histogram.r
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,18 @@
#'
#' # If, however, we want to see the number of votes cast in each
#' # category, we need to weight by the votes variable
#' m + geom_histogram(aes(weight = votes), binwidth = 0.1) + ylab("votes")
#' m +
#' geom_histogram(aes(weight = votes), binwidth = 0.1) +
#' ylab("votes")
#'
#' # For transformed scales, binwidth applies to the transformed data.
#' # The bins have constant width on the transformed scale.
#' m + geom_histogram() + scale_x_log10()
#' m + geom_histogram(binwidth = 0.05) + scale_x_log10()
#' m +
#' geom_histogram() +
#' scale_x_log10()
#' m +
#' geom_histogram(binwidth = 0.05) +
#' scale_x_log10()
#'
#' # For transformed coordinate systems, the binwidth applies to the
#' # raw data. The bins have constant width on the original scale.
Expand All @@ -85,14 +91,20 @@
#' # bar is anchored at zero, and so when transformed becomes negative
#' # infinity. This is not a problem when transforming the scales, because
#' # no observations have 0 ratings.
#' m + geom_histogram(boundary = 0) + coord_trans(x = "log10")
#' m +
#' geom_histogram(boundary = 0) +
#' coord_trans(x = "log10")
#' # Use boundary = 0, to make sure we don't take sqrt of negative values
#' m + geom_histogram(boundary = 0) + coord_trans(x = "sqrt")
#' m +
#' geom_histogram(boundary = 0) +
#' coord_trans(x = "sqrt")
#'
#' # You can also transform the y axis. Remember that the base of the bars
#' # has value 0, so log transformations are not appropriate
#' m <- ggplot(movies, aes(x = rating))
#' m + geom_histogram(binwidth = 0.5) + scale_y_sqrt()
#' m +
#' geom_histogram(binwidth = 0.5) +
#' scale_y_sqrt()
#' }
#'
#' # You can specify a function for calculating binwidth, which is
Expand Down
12 changes: 8 additions & 4 deletions R/geom-jitter.r
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
#' p + geom_jitter(aes(colour = class))
#'
#' # Use smaller width/height to emphasise categories
#' ggplot(mpg, aes(cyl, hwy)) + geom_jitter()
#' ggplot(mpg, aes(cyl, hwy)) + geom_jitter(width = 0.25)
#' ggplot(mpg, aes(cyl, hwy)) +
#' geom_jitter()
#' ggplot(mpg, aes(cyl, hwy)) +
#' geom_jitter(width = 0.25)
#'
#' # Use larger width/height to completely smooth away discreteness
#' ggplot(mpg, aes(cty, hwy)) + geom_jitter()
#' ggplot(mpg, aes(cty, hwy)) + geom_jitter(width = 0.5, height = 0.5)
#' ggplot(mpg, aes(cty, hwy)) +
#' geom_jitter()
#' ggplot(mpg, aes(cty, hwy)) +
#' geom_jitter(width = 0.5, height = 0.5)
geom_jitter <- function(mapping = NULL, data = NULL,
stat = "identity", position = "jitter",
...,
Expand Down
12 changes: 8 additions & 4 deletions R/geom-point.r
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,21 @@
#' # You can create interesting shapes by layering multiple points of
#' # different sizes
#' p <- ggplot(mtcars, aes(mpg, wt, shape = factor(cyl)))
#' p + geom_point(aes(colour = factor(cyl)), size = 4) +
#' p +
#' geom_point(aes(colour = factor(cyl)), size = 4) +
#' geom_point(colour = "grey90", size = 1.5)
#' p + geom_point(colour = "black", size = 4.5) +
#' p +
#' geom_point(colour = "black", size = 4.5) +
#' geom_point(colour = "pink", size = 4) +
#' geom_point(aes(shape = factor(cyl)))
#'
#' # geom_point warns when missing values have been dropped from the data set
#' # and not plotted, you can turn this off by setting na.rm = TRUE
#' mtcars2 <- transform(mtcars, mpg = ifelse(runif(32) < 0.2, NA, mpg))
#' ggplot(mtcars2, aes(wt, mpg)) + geom_point()
#' ggplot(mtcars2, aes(wt, mpg)) + geom_point(na.rm = TRUE)
#' ggplot(mtcars2, aes(wt, mpg)) +
#' geom_point()
#' ggplot(mtcars2, aes(wt, mpg)) +
#' geom_point(na.rm = TRUE)
#' }
geom_point <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
Expand Down
4 changes: 3 additions & 1 deletion R/geom-quantile.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#' @param geom,stat Use to override the default connection between
#' `geom_quantile()` and `stat_quantile()`.
#' @examples
#' m <- ggplot(mpg, aes(displ, 1 / hwy)) + geom_point()
#' m <-
#' ggplot(mpg, aes(displ, 1 / hwy)) +
#' geom_point()
#' m + geom_quantile()
#' m + geom_quantile(quantiles = 0.5)
#' q10 <- seq(0.05, 0.95, by = 0.05)
Expand Down
15 changes: 9 additions & 6 deletions R/geom-rug.r
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@
#'
#' # move the rug tassels to outside the plot
#' # remember to set clip = "off".
#' p + geom_rug(outside = TRUE) +
#' p +
#' geom_rug(outside = TRUE) +
#' coord_cartesian(clip = "off")
#'
#' # set sides to top right, and then move the margins
#' p + geom_rug(outside = TRUE, sides = "tr") +
#' coord_cartesian(clip = "off") +
#' theme(plot.margin = margin(1, 1, 1, 1, "cm"))
#' p +
#' geom_rug(outside = TRUE, sides = "tr") +
#' coord_cartesian(clip = "off") +
#' theme(plot.margin = margin(1, 1, 1, 1, "cm"))
#'
#' # increase the line length and
#' # expand axis to avoid overplotting
#' p + geom_rug(length = unit(0.05, "npc")) +
#' scale_y_continuous(expand = c(0.1, 0.1))
#' p +
#' geom_rug(length = unit(0.05, "npc")) +
#' scale_y_continuous(expand = c(0.1, 0.1))
#'
geom_rug <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
Expand Down
31 changes: 23 additions & 8 deletions R/geom-text.r
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,19 @@
#' p + geom_text(size = 10)
#'
#' # Set aesthetics to fixed value
#' p + geom_point() + geom_text(hjust = 0, nudge_x = 0.05)
#' p + geom_point() + geom_text(vjust = 0, nudge_y = 0.5)
#' p + geom_point() + geom_text(angle = 45)
#' p +
#' geom_point() +
#' geom_text(hjust = 0, nudge_x = 0.05)
#' p +
#' geom_point() +
#' geom_text(vjust = 0, nudge_y = 0.5)
#' p +
#' geom_point() +
#' geom_text(angle = 45)
#' \dontrun{
#' # Doesn't work on all systems
#' p + geom_text(family = "Times New Roman")
#' p +
#' geom_text(family = "Times New Roman")
#' }
#'
#' # Add aesthetic mappings
Expand All @@ -80,18 +87,26 @@
#'
#' p + geom_text(aes(size = wt))
#' # Scale height of text, rather than sqrt(height)
#' p + geom_text(aes(size = wt)) + scale_radius(range = c(3,6))
#' p +
#' geom_text(aes(size = wt)) +
#' scale_radius(range = c(3,6))
#'
#' # You can display expressions by setting parse = TRUE. The
#' # details of the display are described in ?plotmath, but note that
#' # geom_text uses strings, not expressions.
#' p + geom_text(aes(label = paste(wt, "^(", cyl, ")", sep = "")),
#' parse = TRUE)
#' p +
#' geom_text(
#' aes(label = paste(wt, "^(", cyl, ")", sep = "")),
#' parse = TRUE
#' )
#'
#' # Add a text annotation
#' p +
#' geom_text() +
#' annotate("text", label = "plot mpg vs. wt", x = 2, y = 15, size = 8, colour = "red")
#' annotate(
#' "text", label = "plot mpg vs. wt",
#' x = 2, y = 15, size = 8, colour = "red"
#' )
#'
#' \donttest{
#' # Aligning labels and bars --------------------------------------------------
Expand Down
20 changes: 16 additions & 4 deletions R/geom-tile.r
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,30 @@
#' df <- expand.grid(x = 0:5, y = 0:5)
#' df$z <- runif(nrow(df))
#' # default is compatible with geom_tile()
#' ggplot(df, aes(x, y, fill = z)) + geom_raster()
#' ggplot(df, aes(x, y, fill = z)) +
#' geom_raster()
#' # zero padding
#' ggplot(df, aes(x, y, fill = z)) + geom_raster(hjust = 0, vjust = 0)
#' ggplot(df, aes(x, y, fill = z)) +
#' geom_raster(hjust = 0, vjust = 0)
#'
#' # Inspired by the image-density plots of Ken Knoblauch
#' cars <- ggplot(mtcars, aes(mpg, factor(cyl)))
#' cars + geom_point()
#' cars + stat_bin2d(aes(fill = after_stat(count)), binwidth = c(3,1))
#' cars + stat_bin2d(aes(fill = after_stat(density)), binwidth = c(3,1))
#'
#' cars + stat_density(aes(fill = after_stat(density)), geom = "raster", position = "identity")
#' cars + stat_density(aes(fill = after_stat(count)), geom = "raster", position = "identity")
#' cars +
#' stat_density(
#' aes(fill = after_stat(density)),
#' geom = "raster",
#' position = "identity"
#' )
#' cars +
#' stat_density(
#' aes(fill = after_stat(count)),
#' geom = "raster",
#' position = "identity"
#' )
#' }
geom_tile <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
Expand Down
Loading

0 comments on commit 3aa2937

Please sign in to comment.