| Title: | Inspect 'ggplot2' Plots for Automated Grading in Learning Exercises |
|---|---|
| Description: | 'ggcheck' provides functions that inspect 'ggplot2' objects to make it easier for teachers to check that student plots meet expectations. Designed primarily for automated grading via 'gradethis' in interactive 'learnr' tutorials. |
| Authors: | Garrick Aden-Buie [aut, cre] (ORCID: <https://orcid.org/0000-0002-7111-0077>), Garrett Grolemund [ccp, aut] (ORCID: <https://orcid.org/0000-0002-7765-6011>), Nischal Shrestha [aut] (ORCID: <https://orcid.org/0000-0003-3321-1712>), Alexander Rossell Hayes [ctb] (ORCID: <https://orcid.org/0000-0001-9412-0457>), Sara Altman [ctb] (ORCID: <https://orcid.org/0000-0002-2529-5680>), RStudio, PBC [cph, fnd] |
| Maintainer: | Garrick Aden-Buie <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.5 |
| Built: | 2026-06-03 07:30:57 UTC |
| Source: | https://github.com/rstudio/ggcheck |
These functions generate placeholder values.
default_label() can be used as a named argument in uses_labels()
to check that a label matches the result of get_default_labels()
with that name.
default_param() can be used as a named argument in uses_geom_params()
to check that a parameter matched the result of get_default_params()
with that name.
default_label() default_param()default_label() default_param()
A placeholder value to be used within uses_labels()
or uses_geom_params().
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = trans)) + geom_smooth(se = FALSE) + labs(title = "My plot", x = "Weight", y = "MPG") uses_labels(p, x = default_label(), color = default_label()) uses_geom_params(p, "smooth", size = default_param(), se = default_param())require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = trans)) + geom_smooth(se = FALSE) + labs(title = "My plot", x = "Weight", y = "MPG") uses_labels(p, x = default_label(), color = default_label()) uses_geom_params(p, "smooth", size = default_param(), se = default_param())
Which coordinate system does a plot use?
get_coordinate_system(p)get_coordinate_system(p)
p |
A ggplot2 object |
A character string that corresponds to the suffix of a ggplot2
coord_ function, e.g. "cartesian".
Other functions for checking coordinate systems:
uses_coordinate_system()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() + coord_polar() get_coordinate_system(p)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() + coord_polar() get_coordinate_system(p)
get_data returns the data set used by a ggplot object or a single
layer extracted from the object with get_geom_layer.
get_data(p, local_only = FALSE)get_data(p, local_only = FALSE)
p |
A ggplot object or a layer extracted from a ggplot object with
|
local_only |
|
When passed a ggplot object (i.e. a plot), get_data will return only
the data that has been set globally with ggplot.
When passed a single layer from a plot, the behavior of get_data will
depend on the local_only argument passed to .... If
local_only = TRUE, get_data will return only the data set, if
any, that was defined locally in the function that created the layer. If
local_only = FALSE, get_data will return the data used by the
layer, whether or not that data was defined globally in
ggplot or locally.
A data frame. If no data set is found, get_data returns
NULL
Other functions for checking data:
ith_data_is(),
ith_data(),
uses_data()
require(ggplot2) d2 <- head(mpg) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(data = d2, color = "red") + geom_point() get_data(p) get_data(get_geom_layer(p, i = 1))require(ggplot2) d2 <- head(mpg) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(data = d2, color = "red") + geom_point() get_data(p) get_data(get_geom_layer(p, i = 1))
What is the default label for a plot aesthetic?
get_default_labels(p, aes = NULL)get_default_labels(p, aes = NULL)
p |
A ggplot object |
aes |
If |
A named list in which each element is a character string
or NULL.
Strings are returned for aesthetics with a default value.
NULL is returned for aesthetics that do not exist in the plot,
or non-aesthetic labels that do not have a default value, like title.
Other functions for checking labels:
get_labels(),
uses_labels()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class, shape = drv)) + geom_smooth() + labs(title = "My plot", x = "Weight", y = "MPG", color = NULL) # Returns the label the ggplot would create by default for an aesthetic get_default_labels(p, "x") get_default_labels(p, c("x", "y")) get_default_labels(p) # If an aesthetic does not exist, returns NULL get_default_labels(p, "size") # Non-aesthetic labels have no default value, so they also return NULL get_default_labels(p, "title") get_default_labels(p, "comment") # The colo(u)r aesthetic can be matched with or without a u get_default_labels(p, "color") get_default_labels(p, "colour")require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class, shape = drv)) + geom_smooth() + labs(title = "My plot", x = "Weight", y = "MPG", color = NULL) # Returns the label the ggplot would create by default for an aesthetic get_default_labels(p, "x") get_default_labels(p, c("x", "y")) get_default_labels(p) # If an aesthetic does not exist, returns NULL get_default_labels(p, "size") # Non-aesthetic labels have no default value, so they also return NULL get_default_labels(p, "title") get_default_labels(p, "comment") # The colo(u)r aesthetic can be matched with or without a u get_default_labels(p, "color") get_default_labels(p, "colour")
What are the default parameters for a plot layer?
get_default_params(p, geom, params = NULL, i = NULL)get_default_params(p, geom, params = NULL, i = NULL)
p |
A ggplot object |
geom |
A character string found in the suffix of a ggplot2 geom function,
e.g. |
params |
A character vector.
|
i |
A numerical index, e.g. |
A named list of the same length as params, or, if params is
NULL, a named list of default values for all parameters of geom.
Other functions for checking geom parameters:
uses_geom_params()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_smooth(aes(color = class)) # Returns the parameters the ggplot would use by default for a layer get_default_params(p, "smooth", "linetype") get_default_params(p, "smooth", c("se", "level")) get_default_params(p, "smooth") # If a parameter does not exist, returns NULL get_default_params(p, "smooth", "shape") # The colo(u)r aesthetic can be matched with or without a u get_default_params(p, "smooth", "color") get_default_params(p, "smooth", "colour")require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_smooth(aes(color = class)) # Returns the parameters the ggplot would use by default for a layer get_default_params(p, "smooth", "linetype") get_default_params(p, "smooth", c("se", "level")) get_default_params(p, "smooth") # If a parameter does not exist, returns NULL get_default_params(p, "smooth", "shape") # The colo(u)r aesthetic can be matched with or without a u get_default_params(p, "smooth", "color") get_default_params(p, "smooth", "colour")
get_geom_layer returns a geom layer from a plot along with the global data sets
and aesthetic mappings that the layer may inherit from.
get_geom_layer(p, geom = NULL, i = NULL)get_geom_layer(p, geom = NULL, i = NULL)
p |
A ggplot object |
geom |
A character string found in the suffix of a ggplot2 geom function,
e.g. |
i |
A numerical index, e.g. |
Users can specify a layer in one of 3 ways:
By order of appearance with i. The first layer to appear in the
plot (the one drawn first, on the bottom) corresponds to i = 1.
By type of geom with geom. get_geom_layer will return the
first layer that uses the geom.
By a combination of geom and
i. get_geom_layer will return the ith layer that uses the geom.
An object with class layer_to_check to be manipulated further
with ggcheck functions.
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(color = "red") + geom_point(mapping = aes(color = class)) + geom_smooth(se = FALSE) get_geom_layer(p, i = 1) get_geom_layer(p, geom = "smooth") get_geom_layer(p, geom = "point", i = 2)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(color = "red") + geom_point(mapping = aes(color = class)) + geom_smooth(se = FALSE) get_geom_layer(p, i = 1) get_geom_layer(p, geom = "smooth") get_geom_layer(p, geom = "point", i = 2)
get_geoms returns a vector of geom names, written as character
strings, that describes which geoms in which order are used by a plot.
get_geoms(p)get_geoms(p)
p |
A ggplot object |
A vector of character strings. Each element corresponds to the suffix
of a ggplot2 geom_ function, e.g. c("point", "line", "smooth").
Other functions for checking geoms:
get_geoms_stats(),
ith_geom_is(),
ith_geom_stat(),
ith_geom(),
uses_geoms()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() get_geoms(p)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() get_geoms(p)
List the geom and stat combination used by all layers of a plot.
get_geoms_stats(p)get_geoms_stats(p)
p |
A ggplot object |
A list of lists with a GEOM and STAT character. e.g. list(list(GEOM = "point", STAT = "identity"))
Other functions for checking geoms:
get_geoms(),
ith_geom_is(),
ith_geom_stat(),
ith_geom(),
uses_geoms()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() get_geoms_stats(p)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() get_geoms_stats(p)
get_labels() returns a named list of labels,
written as character strings, indicating which labels are used by a plot.
get_labels(p, aes = NULL)get_labels(p, aes = NULL)
p |
A ggplot object |
aes |
If |
Note that get_labels() will return NULL if a label is explicitly set to
NULL or if a requested aesthetic is not present in the plot.
A named list of character strings.
Other functions for checking labels:
get_default_labels(),
uses_labels()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() + labs(x = "Weight", y = "MPG", color = NULL) get_labels(p) get_labels(p, c("x", "y")) # The colo(u)r aesthetic can be matched with or without a u get_labels(p, "color") get_labels(p, "colour")require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() + labs(x = "Weight", y = "MPG", color = NULL) get_labels(p) get_labels(p, c("x", "y")) # The colo(u)r aesthetic can be matched with or without a u get_labels(p, "color") get_labels(p, "colour")
get_mappings returns the mappings used by a ggplot object or a single
layer extracted from the object with get_geom_layer or get_stat_layer.
get_mappings(p, local_only = FALSE)get_mappings(p, local_only = FALSE)
p |
A ggplot object or a layer extracted from a ggplot object with
|
local_only |
|
When passed a ggplot object (i.e. a plot), get_mappings will return
only the mappings that have been set globally with
ggplot. When passed a single layer from a plot, the
behavior of get_mappings will depend on the value of
local_only. If local_only = TRUE, get_mappings will
return only the mappings defined locally in a layer. When local_only =
FALSE, get_mappings will return the combination of global and local
methods that will be used to plot a layer.
A list with class uneval, as returned by aes
Components of the list are either quosures or constants.
Other functions for checking mappings:
identical_aes(),
ith_mappings_use(),
ith_mappings(),
uses_mappings()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) get_mappings(p) get_mappings(get_geom_layer(p, i = 1), local_only = FALSE)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) get_mappings(p) get_mappings(get_geom_layer(p, i = 1), local_only = FALSE)
get_stat_layer returns a stat layer from a plot along with the global data sets
and aesthetic mappings that the layer may inherit from.
get_stat_layer(p, stat = NULL, i = NULL)get_stat_layer(p, stat = NULL, i = NULL)
p |
A ggplot object |
stat |
A character string found in the suffix of a ggplot2 stat function,
e.g. |
i |
A numerical index, e.g. |
Users can specify a layer in one of 3 ways:
By order of appearance with i. The first layer to appear in the
plot (the one drawn first, on the bottom) corresponds to i = 1.
By type of stat with stat. get_stat_layer will return the
first layer that uses the stat
By a combination of stat and
i. get_stat_layer will return the ith layer that uses the stat
An object with class layer_to_check to be manipulated further
with ggcheck functions.
require(ggplot2) p <- ggplot(data = diamonds, aes(price)) + stat_bin(bins = 20, binwidth = 500) get_stat_layer(p, i = 1) get_stat_layer(p, stat = "bin")require(ggplot2) p <- ggplot(data = diamonds, aes(price)) + stat_bin(bins = 20, binwidth = 500) get_stat_layer(p, i = 1) get_stat_layer(p, stat = "bin")
get_stats returns a vector of stats names, written as character
strings, that describes which stats in which order are used by a plot.
get_stats(p)get_stats(p)
p |
A ggplot object |
A vector of character strings. Each element corresponds to the suffix
of a ggplot2 stat_ function, e.g. c("identity", "smooth").
Other functions for checking stats:
ith_stat_is(),
ith_stat(),
uses_stats()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() get_stats(p)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() get_stats(p)
ggplots to check whether they are equalCompare two ggplots to check whether they are equal
## S3 method for class 'ggplot' gradethis_equal(x, y, ...)## S3 method for class 'ggplot' gradethis_equal(x, y, ...)
x, y
|
Two |
... |
Unused |
A logical value of length one, or an internal gradethis error.
gradethis::gradethis_equal() for the generic function.
library(ggplot2) library(ggcheck) library(gradethis) cty_plot <- ggplot(mpg, aes(x = displ, y = cty)) + geom_point() hwy_plot <- ggplot(mpg, aes(x = displ, y = cty)) + geom_point() gradethis_equal(cty_plot, hwy_plot) gradethis_equal(cty_plot, cty_plot)library(ggplot2) library(ggcheck) library(gradethis) cty_plot <- ggplot(mpg, aes(x = displ, y = cty)) + geom_point() hwy_plot <- ggplot(mpg, aes(x = displ, y = cty)) + geom_point() gradethis_equal(cty_plot, hwy_plot) gradethis_equal(cty_plot, cty_plot)
The ggplot2 package uses quosures to record aesthetic mappings. These record
both the mapping described as well as the environment in which the mapping
was described. As a result, it is difficult to compare mappings created by
students in one environment to mappings created on the fly by graders in
another environment. identical_aes facilitates comparison by ignoring
the environments associated with an aesthetic mapping specification. If the
two specifications contain identical expressions, e.g. x = displ,
etc., identical_aes returns TRUE.
identical_aes(a1, a2)identical_aes(a1, a2)
a1 |
The output of |
a2 |
The output of |
TRUE or FALSE
Other functions for checking mappings:
get_mappings(),
ith_mappings_use(),
ith_mappings(),
uses_mappings()
is_ggplot() tests if an object is a ggplot.
stop_if_not_ggplot() signals an error if an object is not
a ggplot.
fail_if_not_ggplot() returns a failing grade if an
object is not a ggplot.
is_ggplot(p) stop_if_not_ggplot(p, message = getOption("ggcheck.error")) fail_if_not_ggplot( p = .result, message = getOption("ggcheck.fail"), env = parent.frame() )is_ggplot(p) stop_if_not_ggplot(p, message = getOption("ggcheck.error")) fail_if_not_ggplot( p = .result, message = getOption("ggcheck.fail"), env = parent.frame() )
p |
An object |
message |
A message to be displayed if |
env |
Environment in which to find |
is_ggplot() returns TRUE if p is a ggplot
object; otherwise it returns FALSE.
stop_if_not_ggplot() returns an error if p is not a
ggplot object; other it invisibly returns NULL.
fail_if_not_ggplot() returns a failing grade if p is
not a ggplot object; other it invisibly returns NULL.
require(ggplot2) p_valid <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() is_ggplot(p_valid) stop_if_not_ggplot(p_valid) fail_if_not_ggplot(p_valid) p_invalid <- geom_point() is_ggplot(p_invalid) ## Not run: stop_if_not_ggplot(p_invalid) ## End(Not run) fail_if_not_ggplot(p_valid)require(ggplot2) p_valid <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() is_ggplot(p_valid) stop_if_not_ggplot(p_valid) fail_if_not_ggplot(p_valid) p_invalid <- geom_point() is_ggplot(p_invalid) ## Not run: stop_if_not_ggplot(p_invalid) ## End(Not run) fail_if_not_ggplot(p_valid)
ith_data returns the data set used by the ith layer.
ith_data(p, i, local_only = FALSE)ith_data(p, i, local_only = FALSE)
p |
A ggplot object or a layer extracted from a ggplot object with
|
i |
A numerical index that corresponds to the first layer of a plot (1), the second layer (2), and so on. |
local_only |
|
If local_only = TRUE, ith_data returns the data set,
if any, that was defined locally in the function that created the ith layer.
If local_only = FALSE, ith_data returns the data used by
the ith layer, whether or not that data was defined globally in
ggplot or locally.
Functions that use the ith_ prefix are designed to eliminate the need
to call get_geom_layer to check a specific layer in a plot, e.g. p
%>% get_geom_layer(geom = "point") %>% get_data().
A data frame. If no data set is found, ith_data returns NULL.
Other functions for checking data:
get_data(),
ith_data_is(),
uses_data()
require(ggplot2) d2 <- head(mpg) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(data = d2, color = "red") + geom_point() ith_data(p, i = 1)require(ggplot2) d2 <- head(mpg) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(data = d2, color = "red") + geom_point() ith_data(p, i = 1)
ith_data_is checks whether the student uses the supplied data set for
the ith layer of their plot.
ith_data_is(p, data, i, local_only = FALSE)ith_data_is(p, data, i, local_only = FALSE)
p |
A ggplot object or a layer extracted from a ggplot object with
|
data |
A data frame |
i |
A numerical index that corresponds to the first layer of a plot (1), the second layer (2), and so on. |
local_only |
|
Functions that use the ith_ prefix are designed to eliminate the need
to call get_geom_layer to check a specific layer in a plot, e.g. p
%>% get_geom_layer(geom = "point") %>% uses_data(mpg).
If local_only = TRUE, ith_data_is will check only the data set,
if any, that was defined locally in the function that created the ith layer.
If local_only = FALSE, ith_data_is will check the data used by
the ith layer, whether or not that data was defined globally in
ggplot or locally.
TRUE or FALSE
Other functions for checking data:
get_data(),
ith_data(),
uses_data()
require(ggplot2) d2 <- head(mpg) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(data = d2, color = "red") + geom_point() ith_data_is(p, data = head(mpg), i = 1)require(ggplot2) d2 <- head(mpg) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(data = d2, color = "red") + geom_point() ith_data_is(p, data = head(mpg), i = 1)
ith_geom returns the type of geom used by the ith layer.
ith_geom(p, i)ith_geom(p, i)
p |
A ggplot object |
i |
A numerical index that corresponds to the first layer of a plot (1), the second layer (2), and so on. |
A character string that corresponds to the suffix of a ggplot2
geom_ function, e.g. "point".
Other functions for checking geoms:
get_geoms_stats(),
get_geoms(),
ith_geom_is(),
ith_geom_stat(),
uses_geoms()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() ith_geom(p, i = 2)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() ith_geom(p, i = 2)
ith_geom_is checks whether the ith layer uses the prescribed type of geom.
ith_geom_is(p, geom, i = 1)ith_geom_is(p, geom, i = 1)
p |
A ggplot object |
geom |
A character string that corresponds to
the suffix of a ggplot2 |
i |
A numerical index that corresponds to the first layer of a plot (1),
the second layer (2), and so on. |
TRUE or FALSE
Other functions for checking geoms:
get_geoms_stats(),
get_geoms(),
ith_geom_stat(),
ith_geom(),
uses_geoms()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() ith_geom_is(p, geom = "smooth", i = 2)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() ith_geom_is(p, geom = "smooth", i = 2)
ith_geom_stat returns the type of geom used by the ith layer
according to a geom/stat combination.
ith_geom_stat(p, i)ith_geom_stat(p, i)
p |
A ggplot object |
i |
A numerical index that corresponds to the first layer of a plot (1), the second layer (2), and so on. |
A list of lists with a GEOM and STAT strings, each corresponding to the suffix of a ggplot2
geom_ function (e.g. "point"), and stat_ function (e.g. "identity").
e.g. list(list(GEOM = "point", STAT = "identity"))
Other functions for checking geoms:
get_geoms_stats(),
get_geoms(),
ith_geom_is(),
ith_geom(),
uses_geoms()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() ith_geom_stat(p, i = 2)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() ith_geom_stat(p, i = 2)
ith_mappings returns the mappings used by a ggplot object or a single
layer extracted from the object with get_geom_layer or get_stat_layer.
ith_mappings(p, i, local_only = FALSE)ith_mappings(p, i, local_only = FALSE)
p |
A ggplot object or a layer extracted from a ggplot object with
|
i |
A numerical index that corresponds to the first layer of a plot (1),
the second layer (2), and so on. |
local_only |
If |
Functions that use the ith_ prefix are
designed to eliminate the need to call get_layer to check a specific
layer in a plot, e.g. p %>% get_geom_layer(geom = "point") %>% get_mappings().
A list with class uneval, as returned by aes
Components of the list are either quosures or constants.
Other functions for checking mappings:
get_mappings(),
identical_aes(),
ith_mappings_use(),
uses_mappings()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() ith_mappings(p, i = 1, local_only = FALSE) ith_mappings(p, i = 1, local_only = TRUE) ith_mappings(p, i = 2, local_only = FALSE)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() ith_mappings(p, i = 1, local_only = FALSE) ith_mappings(p, i = 1, local_only = TRUE) ith_mappings(p, i = 2, local_only = FALSE)
ith_mappings_use checks whether the student uses the supplied mappings
in the ith layer of their plot.
ith_mappings_use(p, mappings, i, local_only = FALSE, exact = FALSE)ith_mappings_use(p, mappings, i, local_only = FALSE, exact = FALSE)
p |
A ggplot object or a layer extracted from a ggplot object with
|
mappings |
One or more aesthetic mappings created with
|
i |
A numerical index that corresponds to the first layer of a plot (1),
the second layer (2), and so on. |
local_only |
If |
exact |
If |
ith_mappings_use ignores whether or not the student supplied
additional mappings as well. Functions that use the ith_ prefix are
designed to eliminate the need to call get_layer to check a specific
layer in a plot, e.g. p
uses_mappings(aes(color = class)).
A logical value
Other functions for checking mappings:
get_mappings(),
identical_aes(),
ith_mappings(),
uses_mappings()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() ith_mappings_use(p, i = 1, aes(x = displ), local_only = FALSE) ith_mappings_use(p, i = 1, aes(x = displ), local_only = TRUE) ith_mappings_use(p, i = 2, aes(x = displ, y = hwy), local_only = FALSE)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() ith_mappings_use(p, i = 1, aes(x = displ), local_only = FALSE) ith_mappings_use(p, i = 1, aes(x = displ), local_only = TRUE) ith_mappings_use(p, i = 2, aes(x = displ, y = hwy), local_only = FALSE)
ith_stat returns the type of stat used by the ith layer.
ith_stat(p, i)ith_stat(p, i)
p |
A ggplot object |
i |
A numerical index that corresponds to the first layer of a plot (1), the second layer (2), and so on. |
A character string that corresponds to the suffix of a ggplot2
stat_ function, e.g. "qq".
Other functions for checking stats:
get_stats(),
ith_stat_is(),
uses_stats()
require(ggplot2) p <- ggplot(data = diamonds, aes(sample = price)) + geom_qq() ith_stat(p, i = 1)require(ggplot2) p <- ggplot(data = diamonds, aes(sample = price)) + geom_qq() ith_stat(p, i = 1)
ith_stat_is checks whether the ith layer uses the prescribed type of stat
ith_stat_is(p, stat, i = 1)ith_stat_is(p, stat, i = 1)
p |
A ggplot object |
stat |
A character string that corresponds to
the suffix of a ggplot2 |
i |
A numerical index that corresponds to the first layer of a plot (1),
the second layer (2), and so on. |
TRUE or FALSE
Other functions for checking stats:
get_stats(),
ith_stat(),
uses_stats()
require(ggplot2) p <- ggplot(data = diamonds, aes(sample = price)) + geom_qq() ith_stat_is(p, i = 1, "qq")require(ggplot2) p <- ggplot(data = diamonds, aes(sample = price)) + geom_qq() ith_stat_is(p, i = 1, "qq")
How many layers are in a plot?
n_layers(p)n_layers(p)
p |
A ggplot object |
Numeric. The number of layers.
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() n_layers(p)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() n_layers(p)
uses_aesthetics checks whether the student used one or more aesthetics.
uses_aesthetics(p, aesthetics, local_only = FALSE, exact = FALSE)uses_aesthetics(p, aesthetics, local_only = FALSE, exact = FALSE)
p |
A ggplot object or a layer extracted from a ggplot object with
|
aesthetics |
character vector of variables to check for, e.g. "x" or c("x") |
local_only |
|
exact |
If |
By default, uses_aesthetics requires that only one of the
aesthetics need to be used. Set exact to TRUE to check if all of
the variables have to be matched exactly.
A logical value.
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) uses_aesthetics(p, "x") uses_aesthetics(p, c("x", "y")) uses_aesthetics(get_geom_layer(p, "point"), c("x", "y", "color"), local_only = TRUE) uses_aesthetics(get_geom_layer(p, "point"), c("x", "y"), local_only = FALSE)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) uses_aesthetics(p, "x") uses_aesthetics(p, c("x", "y")) uses_aesthetics(get_geom_layer(p, "point"), c("x", "y", "color"), local_only = TRUE) uses_aesthetics(get_geom_layer(p, "point"), c("x", "y"), local_only = FALSE)
uses_coordinate_system checks whether a plot uses the coordinate
system you describe. To describe a coordinate system, use the character
string that matches the suffix of the ggplot2 coord_ function that
would make the coordinate system. The default coordinate system for ggplot2
plots is "cartesian".
uses_coordinate_system(p, coordinates)uses_coordinate_system(p, coordinates)
p |
A ggplot2 object |
coordinates |
A character string that corresponds to the suffix of a
ggplot2 |
TRUE or FALSE
Other functions for checking coordinate systems:
get_coordinate_system()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() + coord_polar() uses_coordinate_system(p, coordinates = "polar")require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() + coord_polar() uses_coordinate_system(p, coordinates = "polar")
uses_data checks whether the data set used by a plot or layer matches
the data set provided.
uses_data(p, data, local_only = FALSE)uses_data(p, data, local_only = FALSE)
p |
A ggplot object or a layer extracted from a ggplot object with
|
data |
A data frame |
local_only |
|
When passed a ggplot object (i.e. a plot), uses_data will check only
the data that has been set globally with ggplot.
When passed a single layer from a plot, the behavior of uses_data will
depend on the local_only argument passed to .... If
local_only = TRUE, uses_data will check only the data set, if
any, that was defined locally in the function that created the layer. If
local_only = FALSE, uses_data will check the data used by the
layer, whether or not that data was defined globally in
ggplot or locally.
A data frame.
Other functions for checking data:
get_data(),
ith_data_is(),
ith_data()
require(ggplot2) d2 <- head(mpg) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(data = d2, color = "red") + geom_point() uses_data(p, mpg) uses_data(get_geom_layer(p, i = 1), data = head(mpg))require(ggplot2) d2 <- head(mpg) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(data = d2, color = "red") + geom_point() uses_data(p, mpg) uses_data(get_geom_layer(p, i = 1), data = head(mpg))
uses_extra_mappings checks if a student's plot contains more than the
required aesthetic mappings. Note that we still return TRUE if
the student's plot differs from the required aesthetic mappings because they
are technically extra mappings from required set. We recommend you use
uses_mapping checks for checking required mappings before uses_extra_mappings.
uses_extra_mappings(p, mappings, local_only = FALSE)uses_extra_mappings(p, mappings, local_only = FALSE)
p |
A ggplot object or a layer extracted from a ggplot object with
|
mappings |
One or more aesthetic mappings created with
|
local_only |
If |
A logical value.
require(ggplot2) p <- ggplot(data = diamonds, aes(x = cut, sample = price)) + geom_qq() uses_extra_mappings(p, aes(sample = price))require(ggplot2) p <- ggplot(data = diamonds, aes(x = cut, sample = price)) + geom_qq() uses_extra_mappings(p, aes(sample = price))
uses_geom_params checks that a plot's geom layer uses a specific parameter.
uses_geom_params(p, geom, ..., params = NULL, i = NULL) uses_geom_param(p, geom, ..., params = NULL, i = NULL)uses_geom_params(p, geom, ..., params = NULL, i = NULL) uses_geom_param(p, geom, ..., params = NULL, i = NULL)
p |
A ggplot object |
geom |
A character string found in the suffix of a ggplot2 geom function,
e.g. |
... |
< |
params |
A named list of geom or stat parameter values, e.g.
|
i |
A numerical index, e.g. |
To specify a specific geom layer, either specify using position using the i index or
by using a combination of geom function suffix name and i to check the ith layer that
uses the geom.
The params argument accepts a list that contains geom, stat, or aes
parameters. This offers flexibility in certain situations where setting a
parameter on a geom_ function is actually setting a stat parameter or
aes parameter. For example, in geom_histogram(binwidth = 500), the
binwidth is a stat parameter, while in
geom_histogram(fill = "blue"), the fill is an aes parameter.
uses_geom_params will take this into account and check geom, stat, and
aes parameters.
Note that uses_geom_params() can detect aes parameters, but not aes
mappings. Parameters are set to static values directly within a layer (e.g.
geom_point(color = "blue")), while mappings associate variables in the data with plot aesthetics using
aes() (e.g. geom_point(aes(color = class))).
A named logical vector of the same length as the number of inputs
to ....
Other functions for checking geom parameters:
get_default_params()
require(ggplot2) p <- ggplot(data = diamonds, aes(x = cut, y = price)) + geom_boxplot(varwidth = TRUE, outlier.alpha = 0.01, fill = "blue") uses_geom_params( p, "boxplot", list(varwidth = TRUE, outlier.alpha = 0.01, fill = "blue") ) uses_geom_params( p, "boxplot", varwidth = TRUE, outlier.alpha = 0.01, fill = "blue" ) # Unnamed arguments check that a parameter is set to any value uses_geom_params(p, "boxplot", "fill")require(ggplot2) p <- ggplot(data = diamonds, aes(x = cut, y = price)) + geom_boxplot(varwidth = TRUE, outlier.alpha = 0.01, fill = "blue") uses_geom_params( p, "boxplot", list(varwidth = TRUE, outlier.alpha = 0.01, fill = "blue") ) uses_geom_params( p, "boxplot", varwidth = TRUE, outlier.alpha = 0.01, fill = "blue" ) # Unnamed arguments check that a parameter is set to any value uses_geom_params(p, "boxplot", "fill")
use_geoms tests whether a plot uses one or more geoms created using a geom.
If checking for a layer that is created using a stat function, please use
uses_stats instead.
uses_geoms(p, geoms, stats = NULL, exact = TRUE)uses_geoms(p, geoms, stats = NULL, exact = TRUE)
p |
A ggplot object |
geoms |
A vector of character strings. Each element should correspond to
the suffix of a ggplot2 |
stats |
A character vector to optionally check for the stats corresponding to geoms e.g. c("identity", "smooth") if checking c("point", "smooth") |
exact |
A boolean to indicate whether to use exact matching |
By default, the plot must have the exact geoms or geom/stat combinations and in the same order.
However, if exact is set to FALSE, the plot geoms or geom/stat combinations do not have to be exact.
TRUE or FALSE
Other functions for checking geoms:
get_geoms_stats(),
get_geoms(),
ith_geom_is(),
ith_geom_stat(),
ith_geom()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() uses_geoms(p, geoms = "point") uses_geoms(p, geoms = c("point", "smooth"), exact = TRUE) uses_geoms(p, geoms = c("point", "smooth"), stats = c("identity", "smooth"))require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() uses_geoms(p, geoms = "point") uses_geoms(p, geoms = c("point", "smooth"), exact = TRUE) uses_geoms(p, geoms = c("point", "smooth"), stats = c("identity", "smooth"))
uses_labels() tests whether a plot uses one or more labels.
uses_labels(p, ...)uses_labels(p, ...)
p |
A ggplot object |
... |
< |
Note that uses_labels() will match NULL if a label is explicitly set to
NULL or if a requested aesthetic is not present in the plot.
A named logical vector of the same length as the number of inputs
to ....
Other functions for checking labels:
get_default_labels(),
get_labels()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class, shape = drv)) + geom_smooth() + labs(title = "My plot", x = "Weight", y = "MPG", color = NULL) # Unnamed arguments check if a label is set for the given aesthetic uses_labels(p, "title", "subtitle", "x", "y") # The check will return TRUE for labels set to NULL uses_labels(p, "color") # The check will return TRUE for aesthetics with default labels uses_labels(p, "shape") # Named arguments check if the label matches an expected value uses_labels(p, x = "Weight") uses_labels(p, x = "Weight", y = "MPG", color = NULL) # You can check for default labels with default_label() uses_labels(p, shape = default_label(), x = default_label()) # The colo(u)r aesthetic can be matched with or without a u uses_labels(p, color = NULL) uses_labels(p, colour = NULL) # Inputs can be passed from a list, with or without the !!! operator label_list <- list(x = "Weight", y = "MPG", color = NULL) uses_labels(p, label_list) uses_labels(p, !!!label_list)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class, shape = drv)) + geom_smooth() + labs(title = "My plot", x = "Weight", y = "MPG", color = NULL) # Unnamed arguments check if a label is set for the given aesthetic uses_labels(p, "title", "subtitle", "x", "y") # The check will return TRUE for labels set to NULL uses_labels(p, "color") # The check will return TRUE for aesthetics with default labels uses_labels(p, "shape") # Named arguments check if the label matches an expected value uses_labels(p, x = "Weight") uses_labels(p, x = "Weight", y = "MPG", color = NULL) # You can check for default labels with default_label() uses_labels(p, shape = default_label(), x = default_label()) # The colo(u)r aesthetic can be matched with or without a u uses_labels(p, color = NULL) uses_labels(p, colour = NULL) # Inputs can be passed from a list, with or without the !!! operator label_list <- list(x = "Weight", y = "MPG", color = NULL) uses_labels(p, label_list) uses_labels(p, !!!label_list)
uses_mappings checks whether the student used one or more mappings in
their plot. By default, uses_mappings ignores whether or not the student
also supplied additional mappings. Use uses_extra_mappings to check if they did.
If exact is TRUE, then all of the mappings have to match exactly.
uses_mappings(p, mappings, local_only = FALSE, exact = FALSE)uses_mappings(p, mappings, local_only = FALSE, exact = FALSE)
p |
A ggplot object or a layer extracted from a ggplot object with
|
mappings |
One or more aesthetic mappings created with
|
local_only |
If |
exact |
If |
A logical value.
Other functions for checking mappings:
get_mappings(),
identical_aes(),
ith_mappings_use(),
ith_mappings()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) uses_mappings(p, aes(x = displ)) uses_mappings(get_geom_layer(p, i = 1), aes(x = displ, color = class), local_only = FALSE) uses_mappings(get_geom_layer(p, i = 1), aes(x = displ, color = class), local_only = TRUE) uses_mappings(p, aes(x = displ, y = hwy), exact = TRUE)require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) uses_mappings(p, aes(x = displ)) uses_mappings(get_geom_layer(p, i = 1), aes(x = displ, color = class), local_only = FALSE) uses_mappings(get_geom_layer(p, i = 1), aes(x = displ, color = class), local_only = TRUE) uses_mappings(p, aes(x = displ, y = hwy), exact = TRUE)
uses_stat_param is a mirror function of uses_geom_param but instead of checking a plot's
geom layer, it checks that a plot's stat layer uses a specific stat parameter.
uses_stat_param(p, stat, params, i = NULL)uses_stat_param(p, stat, params, i = NULL)
p |
A ggplot object |
stat |
A character string found in the suffix of a ggplot2 stat function,
e.g. |
params |
A named list of stat or geom parameter values, e.g. |
i |
A numerical index, e.g. |
To specify a specific stat layer, either specify using position using the i index or
by using a combination of stat function suffix name and i to check the ith layer that
uses the stat.
A boolean
require(ggplot2) p <- ggplot(diamonds, aes(carat)) + stat_bin(bins = 200) uses_stat_param(p, stat = "bin", params = list(bins = 200))require(ggplot2) p <- ggplot(diamonds, aes(carat)) + stat_bin(bins = 200) uses_stat_param(p, stat = "bin", params = list(bins = 200))
uses_stats tests whether a plot uses one or more stats in its layers.
uses_stats(p, stats, geoms = NULL, exact = TRUE)uses_stats(p, stats, geoms = NULL, exact = TRUE)
p |
A ggplot object |
stats |
A vector of character strings. Each element should correspond to
the suffix of a ggplot2 |
geoms |
A character vector to optionally check for the geoms corresponding to stats e.g. c("point", "smooth") if checking c("identity", "smooth") |
exact |
if |
By default, the plot must have the exact stats or geom/stat combinations and in the same order.
However, if exact is set to FALSE, the plot stats or geom/stat combinations do not have to be exact.
TRUE or FALSE
Other functions for checking stats:
get_stats(),
ith_stat_is(),
ith_stat()
require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() uses_stats(p, stats = "smooth") uses_stats(p, stats = c("identity", "smooth"), exact = TRUE) uses_stats(p, c("smooth", "identity"), geoms = c("smooth", "point"))require(ggplot2) p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth() uses_stats(p, stats = "smooth") uses_stats(p, stats = c("identity", "smooth"), exact = TRUE) uses_stats(p, c("smooth", "identity"), geoms = c("smooth", "point"))