Type: | Package |
Title: | Interactive 'ggplot2' Layer and Theme Aesthetic Editor |
Version: | 0.4.1 |
Date: | 2024-02-29 |
Author: | Jonathan Sidi [aut, cre] |
Maintainer: | Jonathan Sidi <yonicd@gmail.com> |
Description: | Interactively edit 'ggplot2' layer and theme aesthetics definitions. |
Depends: | R (≥ 4.3.0), ggplot2 (≥ 3.5.0) |
Imports: | plyr, dplyr, tidyr, purrr, rlang, tibble, scales, rstudioapi, shiny, miniUI, shinyBS, colourpicker, shinyAce, utils, graphics, grid, tools, stats |
Suggests: | testthat, covr |
License: | MIT + file LICENSE |
URL: | https://github.com/yonicd/ggedit |
BugReports: | https://github.com/yonicd/ggedit/issues |
LazyData: | true |
NeedsCompilation: | no |
Packaged: | 2024-03-04 14:12:00 UTC; yonis |
RoxygenNote: | 7.3.1 |
Repository: | CRAN |
Date/Publication: | 2024-03-04 14:40:02 UTC |
add layer
Description
add layer from gg object
Usage
## S3 method for class 'gg'
e1 + e2
Arguments
e1 |
plot |
e2 |
layer |
Value
gg
remove layer
Description
remove layer from gg object
Usage
## S3 method for class 'gg'
e1 - e2
Arguments
e1 |
plot |
e2 |
layer |
Value
gg
aesColor
Description
ColorInput UI production for discrete variables.
Usage
aesColour(type, session = NULL)
Arguments
type |
character of label and inputId of element |
session |
shiny session object |
Value
UI object
aesColorCont
Description
ColorInput UI production for continuous variables.
Usage
aesColourCont(type, session = NULL)
Arguments
type |
character of label and inputId of element |
session |
shiny session object |
Value
UI object
aesSelect
Description
SelectizeInput UI production for palletes.
Usage
aesSelect(type, session = NULL)
Arguments
type |
character of label and inputId of element |
session |
shiny session object |
Value
UI object
aesSlide
Description
sliderInput UI production for continuous variables
Usage
aesSlide(type, session = NULL)
Arguments
type |
character of label and inputId of element |
session |
shiny session object |
Value
UI object
arg.value
Description
Creates list of arguments and values in gg layer.
Usage
arg.value(item, obj.elems, session = NULL)
Arguments
item |
character type of gg layer |
obj.elems |
elements in layer |
session |
shiny session object |
Value
list
Try to coerce a ggplot object into a ggedit object
Description
Applied to ggplot objects to use the ploting function of ggedit.
Usage
as.ggedit(plot)
Arguments
plot |
an object |
Value
an object of class ggedit
See Also
Examples
p <- ggplot2::ggplot(iris,ggplot2::aes(x =Sepal.Length,y=Sepal.Width))
p1 <- p +
ggplot2::geom_point(ggplot2::aes(colour=Species)) +
ggplot2::geom_line()
p2 <- p +
ggplot2::geom_point() +
ggplot2::geom_smooth(method='loess')
p3 <- list(p1,p2)
p4 <- as.ggedit(p3)
p4
recasts to gglist
Description
adds gglist class to object
Usage
as.gglist(plot)
## S3 method for class 'gglist'
as.gglist(plot)
## S3 method for class 'list'
as.gglist(plot)
## S3 method for class 'ggplot'
as.gglist(plot)
## S3 method for class 'ggmatrix'
as.gglist(plot)
Arguments
plot |
object |
Value
gglist object
class_layer
Description
Determines the class of the arguments in a gg layer.
Usage
class_layer(p)
Arguments
p |
ggplot |
Value
data.frame
Clone ggplot facet object
Description
Clone ggplot facet object and return either a gg object or the script to parse and evaluate
Usage
cloneFacet(obj, verbose = FALSE)
Arguments
obj |
gg facet wrap or facet_grid object |
verbose |
boolean, toggles to return object or script (TRUE), Default: FALSE |
Value
gg object or script
Examples
obj <- ggplot2::facet_grid(
rows = vars("a", "b"), cols = vars("c", "d"),
scales = 'free', as.table = FALSE, switch = 'x', shrink = FALSE
)
cloneFacet(obj)
cloneFacet(obj, verbose = TRUE)
Creates an independent copy of a ggplot layer object
Description
Creates copies of ggplot layers from within ggplot objects that are independent of the parent object.
Usage
cloneLayer(l, verbose = FALSE, showDefaults = TRUE)
Arguments
l |
ggplot2 object layer |
verbose |
toggle to control if the output is ggproto object (verbose==FALSE,default) or string of layer call (verbose==TRUE) |
showDefaults |
toggle to control if the verbose output shows all the input arguments passed to the proto object (if verbose==FALSE then ignored) |
Details
ggplot objects are comprimsed of layer objects. Once compiled they are part of the plot object environment and if they are changed internally regardless of where they are in the (ie different environment) it will change the original plot. This function allows to create replicates of the plot layers and edit them independent of the original plot. When setting verbose to TRUE function returns the ggplot2 call as a string to paste in regular ggplot script to generate the layer.
Value
ggproto or string object (conditional on verbose)
Examples
p <- ggplot2::ggplot(iris,ggplot2::aes(x =Sepal.Length,y=Sepal.Width))
p <- p +
ggplot2::geom_point(ggplot2::aes(colour='Species')) +
ggplot2::geom_line()
p$layers[[1]]
newLayer <- cloneLayer(l=p$layers[[1]])
(v <- cloneLayer(l=p$layers[[1]],verbose=TRUE))
eval(parse(text=v))
clone root of ggplot object
Description
clone root of ggplot object and return either a new gg object or a string to parse and evaluate
Usage
cloneRoot(obj, verbose = FALSE)
Arguments
obj |
ggplot object |
verbose |
boolean, toggles to return object or script (TRUE), Default: FALSE |
Value
gg object or script
See Also
Examples
cloneRoot(pList$pointSmooth)
cloneRoot(pList$pointSmooth,verbose=TRUE)
Clone ggplot2 scales from compiled ggplot objects
Description
Clone ggplot2 scales from compiled ggplot objects returns not the original call but the nested call
Usage
cloneScales(p, verbose = FALSE)
Arguments
p |
ggplot object |
verbose |
boolean, if TRUE then returns script to eval(parse) if FALSE returns new compiled object Default: FALSE |
Value
ggplot scale or script (depends on verbose)
Examples
# p <- pList$pointSmooth+scale_colour_continuous(low='red')
# p
# pList$pointSmooth+cloneScales(p)
compare
Description
Compare differences theme object e1 (new theme) to theme object e2 (old theme)
Usage
compare(e1, e2, verbose = TRUE)
Arguments
e1 |
theme object |
e2 |
theme object |
verbose |
logical to control if the output is a character of script or a theme object (default TRUE) |
Value
theme object or character depending on verbose
Examples
compare(ggplot2::theme_bw(),ggplot2::theme_get())
compare(ggplot2::theme_bw(),ggplot2::theme_get(),verbose=FALSE)
Convert ggplot object to a string call
Description
Convert ggplot object to a string call
Usage
dput.ggedit(obj, file = "")
Arguments
obj |
compiled ggplot object |
file |
either a character string naming a file or a connection. " " indicates output to the console, Default: " " |
Value
character
Examples
pList$pointSmooth #original compiled plot
this.gg <- dput.ggedit(pList$pointSmooth) #dput the plot
writeLines(this.gg) #show the output
eval(parse(text=this.gg)) #recompile the plot
#add theme change
p <- pList$pointSmooth+theme(panel.background = element_rect(fill='green'))
this.gg<-dput.ggedit(p) #dput the plot
writeLines(this.gg) #show the output
eval(parse(text=this.gg)) #recompile the plot
fetch_aes_ggplotBuild
Description
Converts ggplot layers to list.
Usage
fetch_aes_ggplotBuild(p, geom_list)
Arguments
p |
ggplot |
geom_list |
character vector of geoms in plot |
fetch_layer_aes
Description
Checks the aesthetics in a layer for internal use only.
Usage
fetch_layer_aes(
self,
data = data.frame(x = 1),
params = list(),
stats = list()
)
geom_list
Description
Returns character vector of geom types in ggplot.
Usage
geom_list(p)
Arguments
p |
ggplot |
Value
character vector
ggEditUI
Description
The UI for the ggedit module.
Usage
ggEditUI(id)
Arguments
id |
shiny module id variable |
ggEdit
Description
The ggedit module.
Usage
ggEdit(
input,
output,
session,
obj,
verbose = TRUE,
showDefaults = FALSE,
width = "auto",
height = "auto"
)
Arguments
input |
shinyapp input argument |
output |
shinyapp output argument |
session |
shinyapp session argument |
obj |
ggplot as reactive shiny object |
verbose |
logical to control if the output includes script for layers and themes calls for parsing to create objects (default, verbose=F) |
showDefaults |
toggle to control if the verbose output shows all the input arguments passed to the proto object (if verbose==FALSE then ignored) |
width , height |
Must be a valid CSS unit (like |
retreive all functions that create ggroto layers or stats
Description
Retreive all functions that create ggroto layers or stats
Usage
gg_session(gg_pkg = NULL)
Arguments
gg_pkg |
character, package names to search in if NULL an internal search will look in loaded packages, Default: NULL |
Value
data.frame
Examples
gg_session('ggplot2')
Backcheck what functions created the layers in a ggplot2 plot object
Description
Validate geoms with their unique attributes
Usage
gg_vetting(p, obj = ggedit_opts$get("session_geoms"))
Arguments
p |
gg, compiled ggplot object |
obj |
data.frame, contains the mapping of layer functions as created in gg_session(), Default: ggedit_opts$get('session_geoms') |
Value
data.frame
Examples
gg_vetting(pList$boxplotWrap)
lapply(pList,gg_vetting)
Interactive shiny gadget for editing ggplot layers and themes.
Description
Shiny gadget that takes an input ggplots and populates a user interface with objects that let the user update aesthetics of layers and theme elements.
Usage
ggedit(p.in, ...)
Arguments
p.in |
ggplot plot object or list of objects |
... |
options that are passed to ggedit |
Details
The user can start the gadget using the console ggedit(plotobj)
or through the Addins menu in Rstudio.
If you are using the the Addin option highlight on the editor window the ggplot object and then click the addin.
Options to pass to ggedit
viewer shiny viewer options. It can be either paneViewer (default with minHeight=1000), dialogViewer, browserViewer
verbose logical to control if the output includes script for layers and themes calls for parsing to create objects (default, verbose=TRUE)
showDefaults toggle to control if the verbose output shows all the input arguments passed to the proto object (if verbose==FALSE then ignored)
width,height dimensions of the renderPlot where the active plot is displayed
Once the gadget is running the list of plots are shown in a grid and a number of objects will appear above them.
Action buttons
Cancel:
Returns a NULL object
Done:
Returns the list decribed below.
Dropdown list
Navigates through the plots in the input list. If the input list is a named list the names will be in the dropdown. The plot chosen is termed as the "active plot"
Radio buttons
The options to choose in the radio buttons are the layer names in the active plot.
Links
Update Plot Layer:
A pop up window will appear and be populated with aesthetic elements found in the layer chosen from the radio buttons.
The layer is cloned using cloneLayer
creating a layer independent of the original plot.
If the aesthetic is a factor the values will be shown in dropdown lists.
If it is numeric it will be shown in a slider.
If it is a factor colour/fill aesthetic the colourPicker
package will allow to choose from the full pallete of colours.
If the continuous colour/fill aesthetic a dropdown list will be shown with different palletes
Update Plot Theme:
A popup modal will appear populated with the theme elements found in the active plot. Each element will appear as having a value or empty depending if it was defined or not. The user can change or fill in any element with valid values and any textboxes left empty will use ggplot defaults.
Update Grid Theme:
Copies the theme of the active plot to the other plots in the list
Update Global Theme:
Copies the theme of the active plot to the session theme and all plots created outside of the gadget will have this theme.
View Layer Code:
Opens an ace editor to compare the active layer initial script call and the updated script call.
The ggplot objects returned (layers and themes) can be used on any ggplot object.
Value
List of elements
- updatedPlots
list containing updated ggplot objects
- updatedLayers
For each plot a list of updated layers (ggproto) objects
- UpdatedLayersElements
For each plot a list elements and their values in each layer
- UpdatedLayerCalls
For each plot a list of scripts that can be run directly from the console to create a layer
- updatedScales
For each plot a list of updated scale objects
- UpdatedScalesCalls
For each plot a list of scripts that can be run directly from the console to create a scale object
- updatedThemes
For each plot a list of updated theme objects
- UpdatedThemeCalls
For each plot a list of scripts that can be run directly from the console to create a theme
See Also
cloneLayer
,rgg
,ggplot
,colourPicker
Examples
p <- ggplot2::ggplot(iris,ggplot2::aes(x =Sepal.Length,y=Sepal.Width))
p <- p +
ggplot2::geom_point(ggplot2::aes(colour=Species)) +
ggplot2::geom_line()
if(interactive()){
## Not run:
pnew <- ggedit(p)
pnew
## End(Not run)
}
Default and current ggedit options
Description
Options for functions in the ggedit package. When running R code, the object ggedit_opts
(default options) is not modified by chunk headers (local chunk options are
merged with default options), whereas ggedit_opts_current
(current options)
changes with different chunk headers and it always reflects the options for
the current chunk.
Normally we set up the global options once in the first code chunk in a
document using ggedit_opts$set()
, so that all latter chunks will
use these options. Note the global options set in one chunk will not affect
the options in this chunk itself, and that is why we often need to set global
options in a separate chunk.
Usage
ggedit_opts
ggedit_opts_current
Format
An object of class list
of length 5.
An object of class list
of length 5.
Note
ggedit_opts_current
is read-only in the sense that it does nothing if
you call ggedit_opts_current$set()
; you can only query the options via
ggedit_opts_current$get()
.
Examples
ggedit_opts$get('themeDefaultClass')
gggsave
Description
Wrapper of ggsave that saves ggplot or list of ggplot objects to image or pdf.
Usage
gggsave(filename = "Rplot.pdf", plot = last_plot(), ...)
Arguments
filename |
a character string giving the name of the file. If it is of the form "|cmd", the output is piped to the command given by cmd. If it is NULL, then no external file is created (effectively, no drawing occurs), but the device may still be queried (e.g., for size of text). For use with onefile = FALSE give a C integer format such as "Rplot%03d.pdf" (the default in that case) |
plot |
ggplot or list of ggplots to save, defaults to last plot displayed |
... |
other arguments passed on to graphics device |
Details
default output is to create one pdf regardless of size of list of plots inputted
Value
nothing
Examples
## Not run: gggsave(pList)
is.NullOb
Description
Checks if any columns have a null in them.
Usage
is.NullOb(x)
Arguments
x |
vector or data.frame |
Value
logical
Is the object of class ggedit
Description
Is the object of class ggedit. Very basic for many functions in the package.
Usage
is.ggedit(x)
Arguments
x |
an object |
Value
logical - is the object of class ggedit
gglist
Description
Checks if object is a gglist object
Usage
is.gglist(x)
Arguments
x |
object |
Value
boolean
layersList
Description
Runs the ggplot_build function on the input and converts the output data objects into a nested list with the unique values of each of the aesthetic columns.
Usage
layersList(obj)
Arguments
obj |
ggplot2 plot object or list of plot objects |
Value
list of aesthetics and their values for each layer in a plot
Examples
p=ggplot2::ggplot(iris,ggplot2::aes(x=Sepal.Length,y=Sepal.Width))
p=p+ggplot2::geom_point(ggplot2::aes(colour=Species))+ggplot2::geom_line()
p
p.list=layersList(p)
p.list
layersListFull
Description
Runs the ggplot_build function on the input and converts the output data objects into a nested list with the unique values of each of the aesthetic columns. Includes all objects used for theme plots
Usage
layersListFull(obj)
Arguments
obj |
ggplot2 plot object or list of plot objects |
Value
list of aesthetics and their values for each layer in a plot
layersListObj
Description
return named list for gg layers
Usage
layersListObj(obj, lbl)
List of plots for ggedit examples
Description
List of ggplot objects for examples in ggedit.
Usage
pList
Format
An object of class "list"
Details
list includes a geom_point, geom_point+facet_wrap, geom_boxplot+facet_wrap, geom_point+geom_line, geom_point+geom_smooth, geom_point+geom_line+facet_wrap, geom_point+geom_line+facet_grid
Print ggedit objects
Description
Plots lists of ggplot2 plot objects layout functionality.
Usage
## S3 method for class 'ggedit'
print(x, layout = NULL, byrow = FALSE, ...)
Arguments
x |
list of ggplot2 plot objects |
layout |
matrix, layout of plots like in |
byrow |
boolean, argument passed to default layout (when layout is NULL), used to transpose the output. |
... |
not used |
Examples
p <- as.gglist(pList[1:2])
p
p1 <- p+geom_hline(aes(yintercept=3))
p1
print(p1,byrow=TRUE)
print(p1,layout = matrix(c(2,2,NA,1),ncol=2))
print.gglist
Description
Print method for gglist objects.
Usage
## S3 method for class 'gglist'
print(x, ...)
ggplot2 layer proto extraction
Description
Extract geom, stat and position protos from a ggplot2 layer
Usage
proto_features(l)
Arguments
l |
ggproto |
Value
data.frame
Examples
proto_features(ggplot2::geom_smooth())
proto_features(ggplot2::annotation_logticks())
Remove a layer from a compiled ggplot2 object.
Description
Removes specified layers from a ggplot object.
Usage
remove_geom(p, geom, idx = NULL)
Arguments
p |
ggplot2 plot object |
geom |
character string of the name of the layer to remove |
idx |
numeric of which index of geom to remove, Default: 1 |
Examples
p <- ggplot2::ggplot(iris,ggplot2::aes(x =Sepal.Length,y=Sepal.Width))
p <- p+ggplot2::geom_point(ggplot2::aes(colour=Species))+ggplot2::geom_line()
p
pnew <- p |> remove_geom('point',1)
pnew
Remove and replace ggplot2 layers.
Description
Removes specified layers from a ggplot object and gives the option to replace them with a new layer. This layer can be either a geom object created from regular ggplot functions or an output from the ggedit gadget. In the latter case the layers are found in the updatedLayers object in the ggedit output.
Usage
rgg(p, oldGeom, oldGeomIdx = 1, newLayer = NULL)
Arguments
p |
ggplot2 plot object |
oldGeom |
character string of the name of the layer to remove |
oldGeomIdx |
numeric of which index of OldGeom to remove (default is 1) |
newLayer |
ggplot layer or list of layers |
Examples
p <- ggplot2::ggplot(iris,ggplot2::aes(x =Sepal.Length,y=Sepal.Width))
p <- p+ggplot2::geom_point(ggplot2::aes(colour=Species))+ggplot2::geom_line()
p
p |> rgg('point',1)
if( interactive() ){
x <- ggedit(p)
pnew <- p |> rgg('point',1,x$updatedLayers[[1]])
pnew
}
rmNullObs
Description
Removes null objects from a nested list.
Usage
rmNullObs(x)
Arguments
x |
list |
Value
list
Print to console verbose outputs of objects of class ggedit
Description
function to tidy the ggedit output to single script calls for each plot
Usage
## S3 method for class 'ggedit'
summary(object, ...)
Arguments
object |
ggedit object |
... |
used to pass show.structure |
Details
use show.structure (boolean) to control if the output shows the full data.frame structure or just a placeholder [data.frame], Default: FALSE
themeFetch
Description
returns a nested list of a gg theme object, for ggedit gadget or module.
Usage
themeFetch(a = theme_get())
Arguments
a |
gg theme |
Value
list
themeFetchFull
Description
Returns a nested list of all elements in gg theme object, for plot.theme plot.
Usage
themeFetchFull(a = theme_get())
Arguments
a |
gg theme |
Value
list
themeListDepth
Description
Returns depth of nested list.
Usage
themeListDepth(this, thisdepth = 0)
Arguments
this |
list |
thisdepth |
numeric of current depth in recursive call |
Value
numeric
themeMakePanel
Description
BS modal production for the gg theme.
Usage
themeMakePanel(x, y = NULL, session = NULL)
Arguments
x |
list from themeFetch |
y |
character, names of nested themes elements, Default: NULL |
session |
shiny session object |
Value
UI
themeNewVal
Description
Updates theme with new values from the module UIs.
Usage
themeNewVal(this, p.new, input)
themePanel
Description
UI production for the gg theme.
Usage
themePanel(obj.in, obj.name, session = NULL)
Arguments
obj.in |
theme object in list form |
obj.name |
name of theme object |
session |
shiny session object |
Value
UI
vplayout
Description
Viewport with layout.
Usage
vplayout(x, y)
Arguments
x |
numeric of row number |
y |
numeric of column number |
Value
viewport