Skip to content

Commit 3aa2937

Browse files
authored
Style code used in examples (#4098)
Fixes #4092
1 parent cb0cfd2 commit 3aa2937

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+538
-220
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* Fixed a bug in `labeller()` so that `.default` is passed to `as_labeller()`
44
when labellers are specified by naming faceting variables. (@waltersom, #4031)
5+
6+
* Updated style for example code (@rjake, #4092)
57

68
# ggplot2 3.3.2
79
This is a small release focusing on fixing regressions introduced in 3.3.1.

R/aes-colour-fill-alpha.r

+5-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@
9292
#' p <- ggplot(economics, aes(x = date, y = unemploy)) + geom_line()
9393
#' p
9494
#' yrng <- range(economics$unemploy)
95-
#' p <- p + geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party),
96-
#' ymin = yrng[1], ymax = yrng[2], data = presidential)
95+
#' p <- p +
96+
#' geom_rect(
97+
#' aes(NULL, NULL, xmin = start, xmax = end, fill = party),
98+
#' ymin = yrng[1], ymax = yrng[2], data = presidential
99+
#' )
97100
#' p
98101
#' p + scale_fill_manual(values = alpha(c("blue", "red"), .3))
99102
#' }

R/axis-secondary.R

+26-14
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,39 @@
5959
#'
6060
#' # Secondary axes work for date and datetime scales too:
6161
#' df <- data.frame(
62-
#' dx = seq(as.POSIXct("2012-02-29 12:00:00",
63-
#' tz = "UTC",
64-
#' format = "%Y-%m-%d %H:%M:%S"
65-
#' ),
66-
#' length.out = 10, by = "4 hour"
62+
#' dx = seq(
63+
#' as.POSIXct("2012-02-29 12:00:00", tz = "UTC"),
64+
#' length.out = 10,
65+
#' by = "4 hour"
6766
#' ),
6867
#' price = seq(20, 200000, length.out = 10)
6968
#' )
7069
#'
7170
#' # This may useful for labelling different time scales in the same plot
72-
#' ggplot(df, aes(x = dx, y = price)) + geom_line() +
73-
#' scale_x_datetime("Date", date_labels = "%b %d",
74-
#' date_breaks = "6 hour",
75-
#' sec.axis = dup_axis(name = "Time of Day",
76-
#' labels = scales::time_format("%I %p")))
71+
#' ggplot(df, aes(x = dx, y = price)) +
72+
#' geom_line() +
73+
#' scale_x_datetime(
74+
#' "Date",
75+
#' date_labels = "%b %d",
76+
#' date_breaks = "6 hour",
77+
#' sec.axis = dup_axis(
78+
#' name = "Time of Day",
79+
#' labels = scales::time_format("%I %p")
80+
#' )
81+
#' )
7782
#'
7883
#' # or to transform axes for different timezones
79-
#' ggplot(df, aes(x = dx, y = price)) + geom_line() +
80-
#' scale_x_datetime("GMT", date_labels = "%b %d %I %p",
81-
#' sec.axis = sec_axis(~ . + 8 * 3600, name = "GMT+8",
82-
#' labels = scales::time_format("%b %d %I %p")))
84+
#' ggplot(df, aes(x = dx, y = price)) +
85+
#' geom_line() +
86+
#' scale_x_datetime("
87+
#' GMT",
88+
#' date_labels = "%b %d %I %p",
89+
#' sec.axis = sec_axis(
90+
#' ~ . + 8 * 3600,
91+
#' name = "GMT+8",
92+
#' labels = scales::time_format("%b %d %I %p")
93+
#' )
94+
#' )
8395
#'
8496
#' @export
8597
sec_axis <- function(trans = NULL, name = waiver(), breaks = waiver(), labels = waiver(),

R/geom-dotplot.r

+12-5
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@
6060
#' @references Wilkinson, L. (1999) Dot plots. The American Statistician,
6161
#' 53(3), 276-281.
6262
#' @examples
63-
#' ggplot(mtcars, aes(x = mpg)) + geom_dotplot()
64-
#' ggplot(mtcars, aes(x = mpg)) + geom_dotplot(binwidth = 1.5)
63+
#' ggplot(mtcars, aes(x = mpg)) +
64+
#' geom_dotplot()
65+
#'
66+
#' ggplot(mtcars, aes(x = mpg)) +
67+
#' geom_dotplot(binwidth = 1.5)
6568
#'
6669
#' # Use fixed-width bins
6770
#' ggplot(mtcars, aes(x = mpg)) +
@@ -70,6 +73,7 @@
7073
#' # Some other stacking methods
7174
#' ggplot(mtcars, aes(x = mpg)) +
7275
#' geom_dotplot(binwidth = 1.5, stackdir = "center")
76+
#'
7377
#' ggplot(mtcars, aes(x = mpg)) +
7478
#' geom_dotplot(binwidth = 1.5, stackdir = "centerwhole")
7579
#'
@@ -78,13 +82,16 @@
7882
#' scale_y_continuous(NULL, breaks = NULL)
7983
#'
8084
#' # Overlap dots vertically
81-
#' ggplot(mtcars, aes(x = mpg)) + geom_dotplot(binwidth = 1.5, stackratio = .7)
85+
#' ggplot(mtcars, aes(x = mpg)) +
86+
#' geom_dotplot(binwidth = 1.5, stackratio = .7)
8287
#'
8388
#' # Expand dot diameter
84-
#' ggplot(mtcars, aes(x = mpg)) + geom_dotplot(binwidth = 1.5, dotsize = 1.25)
89+
#' ggplot(mtcars, aes(x = mpg)) +
90+
#' geom_dotplot(binwidth = 1.5, dotsize = 1.25)
8591
#'
8692
#' # Change dot fill colour, stroke width
87-
#' ggplot(mtcars, aes(x = mpg)) + geom_dotplot(binwidth = 1.5, fill = "white", stroke = 2)
93+
#' ggplot(mtcars, aes(x = mpg)) +
94+
#' geom_dotplot(binwidth = 1.5, fill = "white", stroke = 2)
8895
#'
8996
#' \donttest{
9097
#' # Examples with stacking along y axis instead of x

R/geom-errorbarh.r

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
#' # Define the top and bottom of the errorbars
1818
#'
1919
#' p <- ggplot(df, aes(resp, trt, colour = group))
20-
#' p + geom_point() +
20+
#' p +
21+
#' geom_point() +
2122
#' geom_errorbarh(aes(xmax = resp + se, xmin = resp - se))
22-
#' p + geom_point() +
23+
#'
24+
#' p +
25+
#' geom_point() +
2326
#' geom_errorbarh(aes(xmax = resp + se, xmin = resp - se, height = .2))
2427
geom_errorbarh <- function(mapping = NULL, data = NULL,
2528
stat = "identity", position = "identity",

R/geom-function.R

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@
1818
#' geom_function(fun = dnorm, colour = "red")
1919
#'
2020
#' # To plot functions without data, specify range of x-axis
21-
#' base <- ggplot() + xlim(-5, 5)
21+
#' base <-
22+
#' ggplot() +
23+
#' xlim(-5, 5)
24+
#'
2225
#' base + geom_function(fun = dnorm)
26+
#'
2327
#' base + geom_function(fun = dnorm, args = list(mean = 2, sd = .5))
2428
#'
2529
#' # The underlying mechanics evaluate the function at discrete points
2630
#' # and connect the points with lines
2731
#' base + stat_function(fun = dnorm, geom = "point")
32+
#'
2833
#' base + stat_function(fun = dnorm, geom = "point", n = 20)
34+
#'
2935
#' base + geom_function(fun = dnorm, n = 20)
3036
#'
3137
#' # Two functions on the same plot
@@ -35,11 +41,14 @@
3541
#'
3642
#' # Using a custom anonymous function
3743
#' base + geom_function(fun = function(x) 0.5*exp(-abs(x)))
44+
#'
3845
#' base + geom_function(fun = ~ 0.5*exp(-abs(.x)))
3946
#'
4047
#' # Using a custom named function
4148
#' f <- function(x) 0.5*exp(-abs(x))
49+
#'
4250
#' base + geom_function(fun = f)
51+
#'
4352
#' @export
4453
geom_function <- function(mapping = NULL, data = NULL, stat = "function",
4554
position = "identity", ..., na.rm = FALSE,

R/geom-histogram.r

+18-6
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@
7171
#'
7272
#' # If, however, we want to see the number of votes cast in each
7373
#' # category, we need to weight by the votes variable
74-
#' m + geom_histogram(aes(weight = votes), binwidth = 0.1) + ylab("votes")
74+
#' m +
75+
#' geom_histogram(aes(weight = votes), binwidth = 0.1) +
76+
#' ylab("votes")
7577
#'
7678
#' # For transformed scales, binwidth applies to the transformed data.
7779
#' # The bins have constant width on the transformed scale.
78-
#' m + geom_histogram() + scale_x_log10()
79-
#' m + geom_histogram(binwidth = 0.05) + scale_x_log10()
80+
#' m +
81+
#' geom_histogram() +
82+
#' scale_x_log10()
83+
#' m +
84+
#' geom_histogram(binwidth = 0.05) +
85+
#' scale_x_log10()
8086
#'
8187
#' # For transformed coordinate systems, the binwidth applies to the
8288
#' # raw data. The bins have constant width on the original scale.
@@ -85,14 +91,20 @@
8591
#' # bar is anchored at zero, and so when transformed becomes negative
8692
#' # infinity. This is not a problem when transforming the scales, because
8793
#' # no observations have 0 ratings.
88-
#' m + geom_histogram(boundary = 0) + coord_trans(x = "log10")
94+
#' m +
95+
#' geom_histogram(boundary = 0) +
96+
#' coord_trans(x = "log10")
8997
#' # Use boundary = 0, to make sure we don't take sqrt of negative values
90-
#' m + geom_histogram(boundary = 0) + coord_trans(x = "sqrt")
98+
#' m +
99+
#' geom_histogram(boundary = 0) +
100+
#' coord_trans(x = "sqrt")
91101
#'
92102
#' # You can also transform the y axis. Remember that the base of the bars
93103
#' # has value 0, so log transformations are not appropriate
94104
#' m <- ggplot(movies, aes(x = rating))
95-
#' m + geom_histogram(binwidth = 0.5) + scale_y_sqrt()
105+
#' m +
106+
#' geom_histogram(binwidth = 0.5) +
107+
#' scale_y_sqrt()
96108
#' }
97109
#'
98110
#' # You can specify a function for calculating binwidth, which is

R/geom-jitter.r

+8-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@
2323
#' p + geom_jitter(aes(colour = class))
2424
#'
2525
#' # Use smaller width/height to emphasise categories
26-
#' ggplot(mpg, aes(cyl, hwy)) + geom_jitter()
27-
#' ggplot(mpg, aes(cyl, hwy)) + geom_jitter(width = 0.25)
26+
#' ggplot(mpg, aes(cyl, hwy)) +
27+
#' geom_jitter()
28+
#' ggplot(mpg, aes(cyl, hwy)) +
29+
#' geom_jitter(width = 0.25)
2830
#'
2931
#' # Use larger width/height to completely smooth away discreteness
30-
#' ggplot(mpg, aes(cty, hwy)) + geom_jitter()
31-
#' ggplot(mpg, aes(cty, hwy)) + geom_jitter(width = 0.5, height = 0.5)
32+
#' ggplot(mpg, aes(cty, hwy)) +
33+
#' geom_jitter()
34+
#' ggplot(mpg, aes(cty, hwy)) +
35+
#' geom_jitter(width = 0.5, height = 0.5)
3236
geom_jitter <- function(mapping = NULL, data = NULL,
3337
stat = "identity", position = "jitter",
3438
...,

R/geom-point.r

+8-4
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,21 @@
6767
#' # You can create interesting shapes by layering multiple points of
6868
#' # different sizes
6969
#' p <- ggplot(mtcars, aes(mpg, wt, shape = factor(cyl)))
70-
#' p + geom_point(aes(colour = factor(cyl)), size = 4) +
70+
#' p +
71+
#' geom_point(aes(colour = factor(cyl)), size = 4) +
7172
#' geom_point(colour = "grey90", size = 1.5)
72-
#' p + geom_point(colour = "black", size = 4.5) +
73+
#' p +
74+
#' geom_point(colour = "black", size = 4.5) +
7375
#' geom_point(colour = "pink", size = 4) +
7476
#' geom_point(aes(shape = factor(cyl)))
7577
#'
7678
#' # geom_point warns when missing values have been dropped from the data set
7779
#' # and not plotted, you can turn this off by setting na.rm = TRUE
7880
#' mtcars2 <- transform(mtcars, mpg = ifelse(runif(32) < 0.2, NA, mpg))
79-
#' ggplot(mtcars2, aes(wt, mpg)) + geom_point()
80-
#' ggplot(mtcars2, aes(wt, mpg)) + geom_point(na.rm = TRUE)
81+
#' ggplot(mtcars2, aes(wt, mpg)) +
82+
#' geom_point()
83+
#' ggplot(mtcars2, aes(wt, mpg)) +
84+
#' geom_point(na.rm = TRUE)
8185
#' }
8286
geom_point <- function(mapping = NULL, data = NULL,
8387
stat = "identity", position = "identity",

R/geom-quantile.r

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#' @param geom,stat Use to override the default connection between
1414
#' `geom_quantile()` and `stat_quantile()`.
1515
#' @examples
16-
#' m <- ggplot(mpg, aes(displ, 1 / hwy)) + geom_point()
16+
#' m <-
17+
#' ggplot(mpg, aes(displ, 1 / hwy)) +
18+
#' geom_point()
1719
#' m + geom_quantile()
1820
#' m + geom_quantile(quantiles = 0.5)
1921
#' q10 <- seq(0.05, 0.95, by = 0.05)

R/geom-rug.r

+9-6
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,21 @@
3737
#'
3838
#' # move the rug tassels to outside the plot
3939
#' # remember to set clip = "off".
40-
#' p + geom_rug(outside = TRUE) +
40+
#' p +
41+
#' geom_rug(outside = TRUE) +
4142
#' coord_cartesian(clip = "off")
4243
#'
4344
#' # set sides to top right, and then move the margins
44-
#' p + geom_rug(outside = TRUE, sides = "tr") +
45-
#' coord_cartesian(clip = "off") +
46-
#' theme(plot.margin = margin(1, 1, 1, 1, "cm"))
45+
#' p +
46+
#' geom_rug(outside = TRUE, sides = "tr") +
47+
#' coord_cartesian(clip = "off") +
48+
#' theme(plot.margin = margin(1, 1, 1, 1, "cm"))
4749
#'
4850
#' # increase the line length and
4951
#' # expand axis to avoid overplotting
50-
#' p + geom_rug(length = unit(0.05, "npc")) +
51-
#' scale_y_continuous(expand = c(0.1, 0.1))
52+
#' p +
53+
#' geom_rug(length = unit(0.05, "npc")) +
54+
#' scale_y_continuous(expand = c(0.1, 0.1))
5255
#'
5356
geom_rug <- function(mapping = NULL, data = NULL,
5457
stat = "identity", position = "identity",

R/geom-text.r

+23-8
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,19 @@
6464
#' p + geom_text(size = 10)
6565
#'
6666
#' # Set aesthetics to fixed value
67-
#' p + geom_point() + geom_text(hjust = 0, nudge_x = 0.05)
68-
#' p + geom_point() + geom_text(vjust = 0, nudge_y = 0.5)
69-
#' p + geom_point() + geom_text(angle = 45)
67+
#' p +
68+
#' geom_point() +
69+
#' geom_text(hjust = 0, nudge_x = 0.05)
70+
#' p +
71+
#' geom_point() +
72+
#' geom_text(vjust = 0, nudge_y = 0.5)
73+
#' p +
74+
#' geom_point() +
75+
#' geom_text(angle = 45)
7076
#' \dontrun{
7177
#' # Doesn't work on all systems
72-
#' p + geom_text(family = "Times New Roman")
78+
#' p +
79+
#' geom_text(family = "Times New Roman")
7380
#' }
7481
#'
7582
#' # Add aesthetic mappings
@@ -80,18 +87,26 @@
8087
#'
8188
#' p + geom_text(aes(size = wt))
8289
#' # Scale height of text, rather than sqrt(height)
83-
#' p + geom_text(aes(size = wt)) + scale_radius(range = c(3,6))
90+
#' p +
91+
#' geom_text(aes(size = wt)) +
92+
#' scale_radius(range = c(3,6))
8493
#'
8594
#' # You can display expressions by setting parse = TRUE. The
8695
#' # details of the display are described in ?plotmath, but note that
8796
#' # geom_text uses strings, not expressions.
88-
#' p + geom_text(aes(label = paste(wt, "^(", cyl, ")", sep = "")),
89-
#' parse = TRUE)
97+
#' p +
98+
#' geom_text(
99+
#' aes(label = paste(wt, "^(", cyl, ")", sep = "")),
100+
#' parse = TRUE
101+
#' )
90102
#'
91103
#' # Add a text annotation
92104
#' p +
93105
#' geom_text() +
94-
#' annotate("text", label = "plot mpg vs. wt", x = 2, y = 15, size = 8, colour = "red")
106+
#' annotate(
107+
#' "text", label = "plot mpg vs. wt",
108+
#' x = 2, y = 15, size = 8, colour = "red"
109+
#' )
95110
#'
96111
#' \donttest{
97112
#' # Aligning labels and bars --------------------------------------------------

R/geom-tile.r

+16-4
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,30 @@
4242
#' df <- expand.grid(x = 0:5, y = 0:5)
4343
#' df$z <- runif(nrow(df))
4444
#' # default is compatible with geom_tile()
45-
#' ggplot(df, aes(x, y, fill = z)) + geom_raster()
45+
#' ggplot(df, aes(x, y, fill = z)) +
46+
#' geom_raster()
4647
#' # zero padding
47-
#' ggplot(df, aes(x, y, fill = z)) + geom_raster(hjust = 0, vjust = 0)
48+
#' ggplot(df, aes(x, y, fill = z)) +
49+
#' geom_raster(hjust = 0, vjust = 0)
4850
#'
4951
#' # Inspired by the image-density plots of Ken Knoblauch
5052
#' cars <- ggplot(mtcars, aes(mpg, factor(cyl)))
5153
#' cars + geom_point()
5254
#' cars + stat_bin2d(aes(fill = after_stat(count)), binwidth = c(3,1))
5355
#' cars + stat_bin2d(aes(fill = after_stat(density)), binwidth = c(3,1))
5456
#'
55-
#' cars + stat_density(aes(fill = after_stat(density)), geom = "raster", position = "identity")
56-
#' cars + stat_density(aes(fill = after_stat(count)), geom = "raster", position = "identity")
57+
#' cars +
58+
#' stat_density(
59+
#' aes(fill = after_stat(density)),
60+
#' geom = "raster",
61+
#' position = "identity"
62+
#' )
63+
#' cars +
64+
#' stat_density(
65+
#' aes(fill = after_stat(count)),
66+
#' geom = "raster",
67+
#' position = "identity"
68+
#' )
5769
#' }
5870
geom_tile <- function(mapping = NULL, data = NULL,
5971
stat = "identity", position = "identity",

0 commit comments

Comments
 (0)