Title: | Capture the Spirit of Your 'ggplot2' Calls |
---|---|
Description: | Creates a reproducible 'ggplot2' object by storing the data and calls. |
Authors: | Jonathan Carroll [aut, cre] |
Maintainer: | Jonathan Carroll <[email protected]> |
License: | GPL(>=3) |
Version: | 0.2.1 |
Built: | 2024-10-26 03:55:34 UTC |
Source: | https://github.com/jonocarroll/ggghost |
Calls can be removed from the ggghost
object via regex matching of the
function name. All matching calls will be removed based on the match to the
string up to the first bracket, so any arguments are irrelevant.
## S3 method for class 'gg' e1 - e2
## S3 method for class 'gg' e1 - e2
e1 |
An object of class |
e2 |
A component to remove from |
For example, subtracting geom_line()
will remove all calls matching
geom_line
regardless of their arguments.
'labs()' has been identified as a special case, as it requires an argument in order to be recognised as a valid function. Thus, trying to remove it with an empty argument will fail. That said, the argument doesn't need to match, so it can be populated with a dummy string or anything that evaluates in scope. See examples.
A ggghost
structure with calls matching e2
removed,
otherwise the same as e1
## create a ggghost object tmpdata <- data.frame(x = 1:100, y = rnorm(100)) z %g<% ggplot(tmpdata, aes(x,y)) z <- z + geom_point(col = "steelblue") z <- z + theme_bw() z <- z + labs(title = "My cool ggplot") z <- z + labs(x = "x axis", y = "y axis") z <- z + geom_smooth() ## remove the geom_smooth z - geom_smooth() ## remove the labels ## NOTE: argument must be present and able to be ## evaluated in scope z - labs(TRUE) # works z - labs(title) # works because of title(), but removes all labs()
## create a ggghost object tmpdata <- data.frame(x = 1:100, y = rnorm(100)) z %g<% ggplot(tmpdata, aes(x,y)) z <- z + geom_point(col = "steelblue") z <- z + theme_bw() z <- z + labs(title = "My cool ggplot") z <- z + labs(x = "x axis", y = "y axis") z <- z + geom_smooth() ## remove the geom_smooth z - geom_smooth() ## remove the labels ## NOTE: argument must be present and able to be ## evaluated in scope z - labs(TRUE) # works z - labs(title) # works because of title(), but removes all labs()
The data and initial ggpot()
call are stored as a list (call) with
attribute (data).
lhs %g<% rhs
lhs %g<% rhs
lhs |
LHS of call |
rhs |
RHS of call |
The data must be passed into the ggplot
call directly.
Passing this in via a magrittr pipe remains as a future improvement. The
newly created ggghost
object is a list of length 1 containing the
ggplot
call, with attribute data
; another list, containing
the data_name
and data
itself.
Assigns the ggghost
structure to the lhs
symbol.
## create a ggghost object tmpdata <- data.frame(x = 1:100, y = rnorm(100)) z %g<% ggplot(tmpdata, aes(x,y))
## create a ggghost object tmpdata <- data.frame(x = 1:100, y = rnorm(100)) z %g<% ggplot(tmpdata, aes(x,y))
This operator allows you to add objects to a ggghost object in the style of @hrbrmstr.
## S3 method for class 'gg' e1 + e2
## S3 method for class 'gg' e1 + e2
e1 |
An object of class |
e2 |
A component to add to |
Appends the e2
call to the ggghost
structure
#' ## create a ggghost object tmpdata <- data.frame(x = 1:100, y = rnorm(100)) z %g<% ggplot(tmpdata, aes(x,y)) z <- z + geom_point(col = "steelblue") z <- z + theme_bw() z <- z + labs(title = "My cool ggplot") z <- z + labs(x = "x axis", y = "y axis") z <- z + geom_smooth()
#' ## create a ggghost object tmpdata <- data.frame(x = 1:100, y = rnorm(100)) z %g<% ggplot(tmpdata, aes(x,y)) z <- z + geom_point(col = "steelblue") z <- z + theme_bw() z <- z + labs(title = "My cool ggplot") z <- z + labs(x = "x axis", y = "y axis") z <- z + geom_smooth()
Creates a reproducible container for ggplot, storing the data and calls required to produce a plot.
'ggplot' stores the information needed to build the graph as a 'grob', but that's what the **computer** needs to know about in order to build the graph. As humans, we're more interested in what commands were issued in order to build the graph. For good reproducibility, the calls need to be applied to the relevant data. While this is somewhat available by deconstructing the 'grob', it's not the simplest approach.
Here is one option that solves that problem.
'ggghost' stores the data used in a 'ggplot()' call, and collects 'ggplot' commands (usually separated by '+') as they are applied, in effect lazily collecting the calls. Once the object is requested, the 'print' method combines the individual calls back into the total plotting command and executes it. This is where the call would usually be discarded. Instead, a "ghost" of the commands lingers in the object for further investigation, subsetting, adding to, or subtracting from.
Reports whether x is a ggghost object
is.ggghost(x)
is.ggghost(x)
x |
An object to test |
logical; TRUE
if x
inherits class ggghost
Collect ggghost calls and produce the ggplot output
## S3 method for class 'ggghost' print(x, ...)
## S3 method for class 'ggghost' print(x, ...)
x |
A ggghost object to be made into a ggplot grob |
... |
Not used, provided for |
The ggplot plot data (invisibly). Used for the side-effect of producing a ggplot plot.
Creates an animation showing the stepwise process of building up a ggplot. Successively adds calls from a ggghost object and then combines these into an animated GIF.
reanimate(object, gifname = "ggghost.gif", interval = 1, ani.width = 600, ani.height = 600) lazarus(object, gifname = "ggghost.gif", interval = 1, ani.width = 600, ani.height = 600)
reanimate(object, gifname = "ggghost.gif", interval = 1, ani.width = 600, ani.height = 600) lazarus(object, gifname = "ggghost.gif", interval = 1, ani.width = 600, ani.height = 600)
object |
A ggghost object to animate |
gifname |
Output filename to save the .gif to (not including any path, will be saved to current directory) |
interval |
A positive number to set the time interval of the animation
(unit in seconds); see |
ani.width |
width of image frames (unit in px); see
|
ani.height |
height of image frames (unit in px); see
|
TRUE
if it gets that far
## Not run: ## create an animation showing the process of building up a plot reanimate(z, "mycoolplot.gif") ## End(Not run)
## Not run: ## create an animation showing the process of building up a plot reanimate(z, "mycoolplot.gif") ## End(Not run)
The data used to generate a plot is an essential requirement for a
reproducible graphic. This is somewhat available from a ggplot grob
(in raw form) but it it not easily accessible, and isn't named the same way
as the original call.
recover_data(x, supp = TRUE)
recover_data(x, supp = TRUE)
x |
A ggghost object from which to extract the data. |
supp |
(logical) Should the supplementary data be extracted also? |
This function retrieves the data from the ggghost object as it was when it was originally called.
If supplementary data has also been attached using supp_data
then this will also be recovered (if requested).
When used iteractively, a warning will be produced if the data to be extracted exists in the workspace but not identical to the captured version.
A data.frame
of the original data, named as it was when used
in ggplot(data)
Alternative to subtracting calls using '-.gg', this method allows one to select the desired components of the available calls and have those evaluated.
## S3 method for class 'ggghost' subset(x, ...)
## S3 method for class 'ggghost' subset(x, ...)
x |
A ggghost object to subset |
... |
A logical expression indicating which elements to select. Typically a vector of list numbers, but potentially a vector of logicals or logical expressions. |
Another ggghost object containing only the calls selected.
## create a ggghost object tmpdata <- data.frame(x = 1:100, y = rnorm(100)) z %g<% ggplot(tmpdata, aes(x,y)) z <- z + geom_point(col = "steelblue") z <- z + theme_bw() z <- z + labs(title = "My cool ggplot") z <- z + labs(x = "x axis", y = "y axis") z <- z + geom_smooth() ## remove the labels and theme subset(z, c(1,2,6)) ## or subset(z, c(TRUE,TRUE,FALSE,FALSE,FALSE,TRUE))
## create a ggghost object tmpdata <- data.frame(x = 1:100, y = rnorm(100)) z %g<% ggplot(tmpdata, aes(x,y)) z <- z + geom_point(col = "steelblue") z <- z + theme_bw() z <- z + labs(title = "My cool ggplot") z <- z + labs(x = "x axis", y = "y axis") z <- z + geom_smooth() ## remove the labels and theme subset(z, c(1,2,6)) ## or subset(z, c(TRUE,TRUE,FALSE,FALSE,FALSE,TRUE))
Summarises a ggghost object by presenting the contained calls in the order they were added. Optionally concatenates these into a single ggplot call.
## S3 method for class 'ggghost' summary(object, ...)
## S3 method for class 'ggghost' summary(object, ...)
object |
A ggghost object to present |
... |
Mainly provided for |
The data is also included in ggghost objects. If this is also
desired in the output, use str
. See example.
Either a list of ggplot calls or a string of such concatenated with " + "
## present the ggghost object as a list tmpdata <- data.frame(x = 1:100, y = rnorm(100)) z %g<% ggplot(tmpdata, aes(x,y)) z <- z + geom_point(col = "steelblue") summary(z) ## present the ggghost object as a string summary(z, combine = TRUE) # Note, value of 'combine' is arbitrary ## to inspect the data structure also captured, use str() str(z)
## present the ggghost object as a list tmpdata <- data.frame(x = 1:100, y = rnorm(100)) z %g<% ggplot(tmpdata, aes(x,y)) z <- z + geom_point(col = "steelblue") summary(z) ## present the ggghost object as a string summary(z, combine = TRUE) # Note, value of 'combine' is arbitrary ## to inspect the data structure also captured, use str() str(z)
Inspect the supplementary data attached to a ggghost object
supp_data(x)
supp_data(x)
x |
A ggghost object |
A list with two elements: the name of the supplementary data, and the supplementary data itself
Attach supplementary data to a ggghost object
supp_data(x) <- value
supp_data(x) <- value
x |
A ggghost object to which the supplementary data should be attached |
value |
Supplementary data to attach to the ggghost object, probably
used as an additional data input to a |
The original object with suppdata
attribute