Skip to content

Style code used in examples #4098

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 32 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
03fcd70
Style aes_colour_fill_alpha
rjake Jun 26, 2020
a27896a
Style sec_axis
rjake Jun 26, 2020
dd1e7de
Style geom_dotplot
rjake Jun 26, 2020
f0401ad
Style errorbarh
rjake Jun 26, 2020
14ed62a
Style geom_function
rjake Jun 26, 2020
b165897
Style geom_histogram
rjake Jun 26, 2020
77c778f
Style geom_jitter
rjake Jun 26, 2020
848fed1
Style geom_point
rjake Jun 26, 2020
e0588e0
Style geom_quantile
rjake Jun 26, 2020
2ab469d
Style geom_rug
rjake Jun 26, 2020
fec13e0
Style geom_text
rjake Jun 26, 2020
717860b
Style geom_tile
rjake Jun 26, 2020
dcc35c9
Style geom_violin
rjake Jun 26, 2020
da5b96d
Style guide_colorbar
rjake Jun 26, 2020
76e5458
Style guide_legend
rjake Jun 26, 2020
8cc52a5
Style labels
rjake Jun 26, 2020
2bbe3d8
Style layer
rjake Jun 26, 2020
4ccf8f1
Style position_dodge
rjake Jun 26, 2020
5c463a3
Style save
rjake Jun 26, 2020
919fa09
Style scale_brewer
rjake Jun 26, 2020
0d7b261
Style scale_discrete
rjake Jun 26, 2020
c6d7250
Style scale_hue
rjake Jun 26, 2020
5b6aa19
Style guides
rjake Jun 26, 2020
6d6ec2a
Style stat_ecdf
rjake Jun 26, 2020
506b9cc
Style stat_summary
rjake Jun 26, 2020
de849dc
Style summarise_plot
rjake Jun 26, 2020
a9ef9f3
Scale translate_qplot_lattice
rjake Jun 26, 2020
16c5692
Scale plot_construction
rjake Jun 26, 2020
39d5d59
Style theme_defaults
rjake Jun 26, 2020
226e07f
Update pkg oob -> censor (came through automatically)
rjake Jun 26, 2020
0251d19
Update NEWS.md
rjake Jun 26, 2020
bf491cd
Update code with latest version of scales
rjake Jul 1, 2020
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: 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)
Comment on lines +21 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question is a like break after <- should be added or not, but I think either is fine.

c.f. https://style.tidyverse.org/pipes.html#assignment-2

#'
#' 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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This "text", can be placed in the same line of annotate(, but this is also fine.

Short unnamed arguments can also go on the same line as the function name, even if the whole function call spans multiple lines.
https://style.tidyverse.org/syntax.html#long-lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noticed that Ctrl + Enter sometimes errors out at the bottom if the pattern is mismatched about code touching the parentheses or not

# fine
annotate("text", 
         label = "plot mpg vs. wt")

# fine
annotate(
  "text",
  label = "plot mpg vs. wt"
)

# not fine
annotate("text",
  label = "plot mpg vs. wt"
)

Here's an example

library(tidyverse)

mpg %>% 
  mutate(
    x1 = "ok here") %>% 
  mutate(
    x2 = "not ok") # use Ctrl + Enter in this line

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, thanks for the information. I hope this will be fixed on RStudio...

#' 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