diff --git a/dev/articles/extending-ggplot2.html b/dev/articles/extending-ggplot2.html index a9e97e2d6e..18ff5ba448 100644 --- a/dev/articles/extending-ggplot2.html +++ b/dev/articles/extending-ggplot2.html @@ -153,13 +153,11 @@
-A$inc()
+#> [1] 1
+A$inc()
A$x
-#> [1] 2
-A$inc()
+#> [1] 2
+A$inc()
A$inc()
A$x
#> [1] 4
+StatChull <- ggproto("StatChull", Stat, compute_group = function(data, scales) { data[chull(data$x, data$y), , drop = FALSE] @@ -204,7 +202,7 @@
takes care of teasing the different parameters apart and making sure they’re stored in the right place: -The simplest statlayer()
+stat_chull <- function(mapping = NULL, data = NULL, geom = "polygon", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...) { @@ -218,7 +216,7 @@
explicitly, or import theThe simplest statggplot2::layer()
layer()
function into your package namespace.)Once we have a layer function we can try our new stat:
-+@@ -230,7 +228,7 @@ggplot(mpg, aes(displ, hwy)) + geom_point() + stat_chull(fill = NA, colour = "black")
The simplest stat
+@@ -240,7 +238,7 @@ggplot(mpg, aes(displ, hwy, colour = drv)) + geom_point() + stat_chull(fill = NA)
The simplest stat
We can also override the default geom to display the convex hull in a different way:
-+@@ -255,7 +253,7 @@ggplot(mpg, aes(displ, hwy)) + stat_chull(geom = "point", size = 4, colour = "red") + geom_point()
Stat parametersgeom_smooth() that adds a line of best fit to a plot. We create a
StatLm
that inherits fromStat
and a layer function,stat_lm()
: -+StatLm <- ggproto("StatLm", Stat, required_aes = c("x", "y"), @@ -290,7 +288,7 @@
method and our wrapper function: -Stat parameterscompute_group()
+StatLm <- ggproto("StatLm", Stat, required_aes = c("x", "y"), @@ -331,7 +329,7 @@
: that will automatically inherit documentation for all the parameters also defined forStat parameters@inheritParams ggplot2::stat_identity
stat_identity()
. -+#' @export #' @inheritParams ggplot2::stat_identity #' @param formula The modelling formula passed to \code{lm}. Should only @@ -365,7 +363,7 @@
Picking defaults
To do this we override the
-setup_params()
method. It’s passed the data and a list of params, and returns an updated list.+StatDensityCommon <- ggproto("StatDensityCommon", Stat, required_aes = "x", @@ -405,7 +403,7 @@
Picking defaults -
+ @@ -426,7 +424,7 @@Variable names and default aesthe
default_aes
to automatically mapdensity
toy
, which allows the user to override it to use with different geoms: -+StatDensityCommon <- ggproto("StatDensity2", Stat, required_aes = "x", default_aes = aes(y = after_stat(density)), @@ -445,7 +443,7 @@
Variable names and default aesthe the kernel density estimate of the displacement." width="672" style="display: block; margin: auto;">
However, using this stat with the area geom doesn’t work quite right. The areas don’t stack on top of each other:
-+ggplot(mpg, aes(displ, fill = drv)) + stat_density_common(bandwidth = 1, geom = "area", position = "stack")
Variable names and default aesthe
This is because each density is computed independently, and the estimated
-x
s don’t line up. We can resolve that issue by computing the range of the data once insetup_params()
.+StatDensityCommon <- ggproto("StatDensityCommon", Stat, required_aes = "x", default_aes = aes(y = after_stat(density)), @@ -484,7 +482,7 @@
Variable names and default aesthe Three areas are shown that indicate the estimates for three types of drive trains separately. The areas are stacked on top of one another and show no overlap." width="672" style="display: block; margin: auto;"> -
+ggplot(mpg, aes(displ, drv, fill = after_stat(density))) + stat_density_common(bandwidth = 1, geom = "raster")
A simple geomgeom_point():
-+GeomSimplePoint <- ggproto("GeomSimplePoint", Geom, required_aes = c("x", "y"), default_aes = aes(shape = 19, colour = "black"), @@ -609,7 +607,7 @@
.Collective geomsdraw_group()
The following code makes a simplified version of
-GeomPolygon
:+GeomSimplePolygon <- ggproto("GeomPolygon", Geom, required_aes = c("x", "y"), @@ -687,7 +685,7 @@
to work better withInheriting from an existing GeomGeomPolygon
StatChull
: -+GeomPolygonHollow <- ggproto("GeomPolygonHollow", GeomPolygon, default_aes = aes(colour = "black", fill = NA, linewidth = 0.5, linetype = 1, alpha = NA) @@ -744,7 +742,7 @@
Omnidirectional statsThe actual guessing of orientation will happen in
setup_params()
using thehas_flipped_aes()
helper: -+StatBoxplot$setup_params #> <ggproto method> #> <Wrapper function> @@ -775,7 +773,7 @@
: -Omnidirectional statssetup_data()
+StatBoxplot$setup_data #> <ggproto method> #> <Wrapper function> @@ -811,7 +809,7 @@
): -Omnidirecitonal geomsstat_identity()
+GeomBoxplot$setup_data #> <ggproto method> #> <Wrapper function> @@ -865,7 +863,7 @@
andDealing with required aestheticsStat
Geom
classes understands the|
(or) operator. Looking atGeomBoxplot
we can see how it is used: -+@@ -885,7 +883,7 @@GeomBoxplot$required_aes #> [1] "x|y" "lower|xlower" "upper|xupper" "middle|xmiddle" #> [5] "ymin|xmin" "ymax|xmax"
Ambiguous layersambiguous = TRUE to cancel any guessing based on data format. As an example we can see the
setup_params()
method ofGeomLine
: -+GeomLine$setup_params #> <ggproto method> #> <Wrapper function> @@ -917,11 +915,10 @@
Overriding elements
+-theme_grey()$legend.key -#> NULL
-+#> NULL + new_theme <- theme_grey() + theme(legend.key = element_rect(colour = "red")) new_theme$legend.key #> List of 5 @@ -933,7 +930,7 @@
Overriding elements#> - attr(*, "class")= chr [1:2] "element_rect" "element"
To override it completely, use
-%+replace%
instead of+
:+new_theme <- theme_grey() %+replace% theme(legend.key = element_rect(colour = "red")) new_theme$legend.key #> List of 5 @@ -986,7 +983,7 @@
Global elementsThese set default properties that are inherited by more specific settings. These are most useful for setting an overall “background” colour and overall font settings (e.g. family and size). -
+-df <- data.frame(x = 1:3, y = 1:3) base <- ggplot(df, aes(x, y)) + geom_point() + @@ -995,7 +992,7 @@
Global elementsbase
+@@ -1076,7 +1073,7 @@base + theme(text = element_text(colour = "red"))
Creating a layout specificationfacet_grid() will e.g. also return the faceting variables associated with each panel). Let’s make a function that defines a duplicate layout: -
+@@ -1090,7 +1087,7 @@layout <- function(data, params) { data.frame(PANEL = c(1L, 2L), SCALE_X = 1L, SCALE_Y = 1L) }
Mapping data into panelsPANEL column to the layer data identifying which panel it belongs to. -
+mapping <- function(data, layout, params) { if (is.null(data) || nrow(data) == 0) { return(cbind(data, PANEL = integer(0))) @@ -1111,7 +1108,7 @@
Laying out the panelsWhile the two functions above have been deceivingly simple, this last one is going to take some more work. Our goal is to draw two panels beside (or above) each other with axes etc. -
+render <- function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, params) { # Place panels according to settings @@ -1187,7 +1184,7 @@
Assembling the Facet class -
+# Constructor: shrink is required to govern whether scales are trained on # Stat-transformed data or not. facet_duplicate <- function(horizontal = TRUE, shrink = TRUE) { @@ -1205,11 +1202,11 @@
Assembling the Facet class draw_panels = render )
Now with everything assembled, lets test it out:
-+-p <- ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point() p
+@@ -1220,7 +1217,7 @@p + facet_duplicate()
Doing more with facetsThe example above was pretty useless and we’ll now try to expand on it to add some actual usability. We are going to make a faceting that adds panels with y-transformed axes: -
+library(scales) facet_trans <- function(trans, horizontal = TRUE, shrink = TRUE) { @@ -1403,7 +1400,7 @@
Doing more with facets
Enough talk - lets see if our new and powerful faceting extension works:
-+ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point() + facet_trans('sqrt')
Extending existing facet functionfacet_wrap() to make a
-facet_bootstrap()
class that splits the input data into a number of panels at random.+facet_bootstrap <- function(n = 9, prop = 0.2, nrow = NULL, ncol = NULL, scales = "fixed", shrink = TRUE, strip.position = "top") { @@ -1510,7 +1507,7 @@
Creating new guides
+p <- ggplot(mpg, aes(displ, hwy, colour = drv)) + geom_point() + scale_colour_discrete( @@ -1533,7 +1530,7 @@
Overriding scale extraction
+GuideKey <- ggproto( "Guide", GuideAxis, @@ -1558,7 +1555,7 @@
Guide constructors
+guide_key <- function( aesthetic, value = aesthetic, label = as.character(aesthetic), ..., @@ -1579,7 +1576,7 @@
Guide constructors}
Our new guide can now be used inside the
-guides()
function or as theguide
argument in a position scale.+ggplot(mpg, aes(displ, hwy)) + geom_point() + scale_x_continuous( @@ -1609,7 +1606,7 @@
parameter. -Custom drawingsposition
+# Same as before GuideKey <- ggproto( "Guide", GuideAxis, @@ -1639,7 +1636,7 @@
in the key, adding aCustom drawingsguide_key()
colour
column to the key is straightforward. We can check that are guide looks correct in the different positions around the panel. -+-ggplot(mpg, aes(displ, hwy)) + geom_point() + guides( diff --git a/dev/articles/faq-faceting.html b/dev/articles/faq-faceting.html index ddb5b3d5eb..211d1b915a 100644 --- a/dev/articles/faq-faceting.html +++ b/dev/articles/faq-faceting.html @@ -220,9 +220,8 @@
#> filter, lag #> The following objects are masked from 'package:base': #> -#> intersect, setdiff, setequal, union
-+#> intersect, setdiff, setequal, union + mpg_summary <- mpg %>% group_by(drv) %>% summarise(hwy_mean = mean(hwy)) @@ -236,7 +235,7 @@
#> 3 r 21
Then, add a
-geom_vline()
layer to your plot that uses the summary data.+ggplot(mpg, aes(x = hwy)) + geom_histogram(binwidth = 5) + facet_wrap(~ drv) + @@ -263,7 +262,7 @@
How can I set individua See example
Suppose you have the following faceted plot. By default, both x and y scales are shared across the facets.
-+@@ -277,7 +276,7 @@ggplot(mpg, aes(x = cty, y = hwy)) + geom_point() + facet_grid(cyl ~ drv)
How can I set individua of faceting functions: varying scales across rows (
"free_x"
), columns ("free_y"
), or both rows and columns ("free"
), e.g. -+@@ -292,7 +291,7 @@ggplot(mpg, aes(x = cty, y = hwy)) + geom_point() + facet_grid(cyl ~ drv, scales = "free")
How can I set individua included in each of the facets, you can set this with
expand_limits()
, e.g. ensure that 10 is included in the x-axis and values between 20 to 25 are included in the y-axis: -+ggplot(mpg, aes(x = cty, y = hwy)) + geom_point() + facet_grid(cyl ~ drv, scales = "free") + @@ -319,7 +318,7 @@
How can I remove the facet l See example
Setting
-strip.text
toelement_blank()
will remove all facet labels.+ggplot(mpg, aes(x = cty, y = hwy)) + geom_point() + facet_grid(cyl ~ drv) + @@ -331,7 +330,7 @@
How can I remove the facet l
You can also remove the labels across rows only with
-strip.x.text
or across columns only withstrip.y.text
.+ggplot(mpg, aes(x = cty, y = hwy)) + geom_point() + facet_grid(cyl ~ drv) + @@ -357,7 +356,7 @@
+df <- data.frame( x = rnorm(100), group = c(rep("A long group name for the first group", 50), @@ -376,7 +375,7 @@
function, which is then passed to thelabel_wrap_gen()
labeller
argument of your faceting function. -+@@ -400,7 +399,7 @@ggplot(df, aes(x = x)) + geom_histogram(binwidth = 0.5) + facet_wrap(~ group, labeller = labeller(group = label_wrap_gen(width = 25)))
How can I set different See example
Suppose you have data price data on a given item over a few years from two countries with very different currency scales.
-+df <- data.frame( year = rep(2016:2021, 2), price = c(10, 10, 13, 12, 14, 15, 1000, 1010, 1200, 1050, 1105, 1300), @@ -424,7 +423,7 @@
How can I set different
You can plot
-price
versustime
and facet bycountry
, but the resulting plot can be a bit difficult to read due to the shared y-axis label.+ggplot(df, aes(x = year, y = price)) + geom_smooth() + facet_wrap(~ country, ncol = 1, scales = "free_y") + @@ -437,7 +436,7 @@
How can I set different
as_labeller()
, turn off the default y-axis label, and then place the facet labels where the y-axis label goes ("outside"
and on the"left"
). -+-ggplot(df, aes(x = year, y = price)) + geom_smooth() + facet_wrap(~ country, ncol = 1, scales = "free_y", diff --git a/dev/articles/profiling.html b/dev/articles/profiling.html index e0768c8abb..efe1b80fc2 100644 --- a/dev/articles/profiling.html +++ b/dev/articles/profiling.html @@ -130,7 +130,7 @@
Thomas Lin profile
In general, a minimal plot is used so that profiles are focused on +
In general, a minimal plot is used so that profiles are focused on low-level, general code, rather than implementations of specific geoms. This might be expanded at the point where improving performance of specific geoms becomes a focus. Further, the profile focuses on the diff --git a/dev/news/index.html b/dev/news/index.html index 48831cb155..5d27fe9794 100644 --- a/dev/news/index.html +++ b/dev/news/index.html @@ -67,7 +67,8 @@
ggplot2 (development version)
-
- +
All position scales now use the same definition of
x
andy
aesthetics. This lets uncommon aesthetics likexintercept
expand scales as usual. (#3342, #4966, @teunbrand)
- +
geom_rect()
can now derive the required corners positions fromx
/width
ory
/height
parameterisation (@teunbrand, #5861).All position scales now use the same definition of
x
andy
aesthetics. This lets uncommon aesthetics likexintercept
expand scales as usual. (#3342, #4966, @teunbrand)Bare numeric values provided to Date or Datetime scales get inversely transformed (cast to Date/POSIXct) with a warning (@teunbrand).
stat_bin()
now accepts functions for argumentbreaks
(@aijordan, #4561)- diff --git a/dev/pkgdown.yml b/dev/pkgdown.yml index 64b965a898..91007694eb 100644 --- a/dev/pkgdown.yml +++ b/dev/pkgdown.yml @@ -13,7 +13,7 @@ articles: ggplot2-specs: ggplot2-specs.html ggplot2: ggplot2.html profiling: profiling.html -last_built: 2024-07-08T09:16Z +last_built: 2024-07-10T08:01Z urls: reference: https://ggplot2.tidyverse.org/reference article: https://ggplot2.tidyverse.org/articles diff --git a/dev/reference/benchplot.html b/dev/reference/benchplot.html index cd6e3dfba2..379fa9abba 100644 --- a/dev/reference/benchplot.html +++ b/dev/reference/benchplot.html @@ -91,29 +91,29 @@
(internal) The plot’s layout now has a coord parameter that is used to prevent setting up identical panel parameters (#5427)
ArgumentsExamples
benchplot(ggplot(mtcars, aes(mpg, wt)) + geom_point()) #> step user.self sys.self elapsed -#> 1 construct 0.003 0 0.003 -#> 2 build 0.018 0 0.018 -#> 3 render 0.019 0 0.019 -#> 4 draw 0.020 0 0.020 -#> 5 TOTAL 0.060 0 0.060 +#> 1 construct 0.000 0.002 0.003 +#> 2 build 0.017 0.000 0.017 +#> 3 render 0.021 0.000 0.021 +#> 4 draw 0.020 0.000 0.020 +#> 5 TOTAL 0.058 0.002 0.061 benchplot(ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(. ~ cyl)) #> step user.self sys.self elapsed -#> 1 construct 0.002 0.001 0.002 -#> 2 build 0.020 0.000 0.019 -#> 3 render 0.044 0.000 0.044 -#> 4 draw 0.035 0.000 0.035 -#> 5 TOTAL 0.101 0.001 0.100 +#> 1 construct 0.003 0 0.003 +#> 2 build 0.020 0 0.020 +#> 3 render 0.044 0 0.044 +#> 4 draw 0.034 0 0.034 +#> 5 TOTAL 0.101 0 0.101 # With tidy eval: p <- expr(ggplot(mtcars, aes(mpg, wt)) + geom_point()) benchplot(!!p) #> step user.self sys.self elapsed -#> 1 construct 0.002 0 0.003 -#> 2 build 0.017 0 0.018 -#> 3 render 0.018 0 0.019 -#> 4 draw 0.021 0 0.020 -#> 5 TOTAL 0.058 0 0.060 +#> 1 construct 0.003 0 0.002 +#> 2 build 0.018 0 0.018 +#> 3 render 0.019 0 0.019 +#> 4 draw 0.020 0 0.020 +#> 5 TOTAL 0.060 0 0.059