Title: | Interactive 'ggplot2' Layer and Theme Aesthetic Editor |
---|---|
Description: | Interactively edit 'ggplot2' layer and theme aesthetics definitions. |
Authors: | Jonathan Sidi [aut, cre] |
Maintainer: | Jonathan Sidi <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4.2 |
Built: | 2024-11-07 04:58:03 UTC |
Source: | https://github.com/yonicd/ggedit |
remove layer from gg object
## S3 method for class 'gg' e1 - e2
## S3 method for class 'gg' e1 - e2
e1 |
plot |
e2 |
layer |
gg
add layer from gg object
## S3 method for class 'gg' e1 + e2
## S3 method for class 'gg' e1 + e2
e1 |
plot |
e2 |
layer |
gg
Applied to ggplot objects to use the ploting function of ggedit.
as.ggedit(plot)
as.ggedit(plot)
plot |
an object |
an object of class ggedit
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
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
adds gglist class to object
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)
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)
plot |
object |
gglist object
Clone ggplot facet object and return either a gg object or the script to parse and evaluate
cloneFacet(obj, verbose = FALSE)
cloneFacet(obj, verbose = FALSE)
obj |
gg facet wrap or facet_grid object |
verbose |
boolean, toggles to return object or script (TRUE), Default: FALSE |
gg object or script
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)
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 copies of ggplot layers from within ggplot objects that are independent of the parent object.
cloneLayer(l, verbose = FALSE, showDefaults = TRUE)
cloneLayer(l, verbose = FALSE, showDefaults = TRUE)
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) |
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.
ggproto or string object (conditional on verbose)
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))
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 and return either a new gg object or a string to parse and evaluate
cloneRoot(obj, verbose = FALSE)
cloneRoot(obj, verbose = FALSE)
obj |
ggplot object |
verbose |
boolean, toggles to return object or script (TRUE), Default: FALSE |
gg object or script
cloneRoot(pList$pointSmooth) cloneRoot(pList$pointSmooth,verbose=TRUE)
cloneRoot(pList$pointSmooth) cloneRoot(pList$pointSmooth,verbose=TRUE)
Clone ggplot2 scales from compiled ggplot objects returns not the original call but the nested call
cloneScales(p, verbose = FALSE)
cloneScales(p, verbose = FALSE)
p |
ggplot object |
verbose |
boolean, if TRUE then returns script to eval(parse) if FALSE returns new compiled object Default: FALSE |
ggplot scale or script (depends on verbose)
# p <- pList$pointSmooth+scale_colour_continuous(low='red') # p # pList$pointSmooth+cloneScales(p)
# p <- pList$pointSmooth+scale_colour_continuous(low='red') # p # pList$pointSmooth+cloneScales(p)
Compare differences theme object e1 (new theme) to theme object e2 (old theme)
compare(e1, e2, verbose = TRUE)
compare(e1, e2, verbose = TRUE)
e1 |
theme object |
e2 |
theme object |
verbose |
logical to control if the output is a character of script or a theme object (default TRUE) |
theme object or character depending on verbose
compare(ggplot2::theme_bw(),ggplot2::theme_get()) compare(ggplot2::theme_bw(),ggplot2::theme_get(),verbose=FALSE)
compare(ggplot2::theme_bw(),ggplot2::theme_get()) compare(ggplot2::theme_bw(),ggplot2::theme_get(),verbose=FALSE)
Convert ggplot object to a string call
dput.ggedit(obj, file = "")
dput.ggedit(obj, file = "")
obj |
compiled ggplot object |
file |
either a character string naming a file or a connection. " " indicates output to the console, Default: " " |
character
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
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
Retreive all functions that create ggroto layers or stats
gg_session(gg_pkg = NULL)
gg_session(gg_pkg = NULL)
gg_pkg |
character, package names to search in if NULL an internal search will look in loaded packages, Default: NULL |
data.frame
gg_session('ggplot2')
gg_session('ggplot2')
Validate geoms with their unique attributes
gg_vetting(p, obj = ggedit_opts$get("session_geoms"))
gg_vetting(p, obj = ggedit_opts$get("session_geoms"))
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') |
data.frame
gg_vetting(pList$boxplotWrap) lapply(pList,gg_vetting)
gg_vetting(pList$boxplotWrap) lapply(pList,gg_vetting)
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.
ggedit(p.in, ...)
ggedit(p.in, ...)
p.in |
ggplot plot object or list of objects |
... |
options that are passed to ggedit |
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.
List of elements
list containing updated ggplot objects
For each plot a list of updated layers (ggproto) objects
For each plot a list elements and their values in each layer
For each plot a list of scripts that can be run directly from the console to create a layer
For each plot a list of updated scale objects
For each plot a list of scripts that can be run directly from the console to create a scale object
For each plot a list of updated theme objects
For each plot a list of scripts that can be run directly from the console to create a theme
cloneLayer
,rgg
,ggplot
,colourPicker
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) }
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) }
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.
ggedit_opts ggedit_opts_current
ggedit_opts ggedit_opts_current
An object of class list
of length 5.
An object of class list
of length 5.
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()
.
ggedit_opts$get('themeDefaultClass')
ggedit_opts$get('themeDefaultClass')
Wrapper of ggsave that saves ggplot or list of ggplot objects to image or pdf.
gggsave(filename = "Rplot.pdf", plot = last_plot(), ...)
gggsave(filename = "Rplot.pdf", plot = last_plot(), ...)
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 |
default output is to create one pdf regardless of size of list of plots inputted
nothing
## Not run: gggsave(pList)
## Not run: gggsave(pList)
Is the object of class ggedit. Very basic for many functions in the package.
is.ggedit(x)
is.ggedit(x)
x |
an object |
logical - is the object of class ggedit
Checks if object is a gglist object
is.gglist(x)
is.gglist(x)
x |
object |
boolean
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.
layersList(obj)
layersList(obj)
obj |
ggplot2 plot object or list of plot objects |
list of aesthetics and their values for each layer in a plot
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
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
List of ggplot objects for examples in ggedit.
pList
pList
An object of class "list"
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
Plots lists of ggplot2 plot objects layout functionality.
## S3 method for class 'ggedit' print(x, layout = NULL, byrow = FALSE, ...)
## S3 method for class 'ggedit' print(x, layout = NULL, byrow = FALSE, ...)
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 |
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))
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))
Extract geom, stat and position protos from a ggplot2 layer
proto_features(l)
proto_features(l)
l |
ggproto |
data.frame
proto_features(ggplot2::geom_smooth()) proto_features(ggplot2::annotation_logticks())
proto_features(ggplot2::geom_smooth()) proto_features(ggplot2::annotation_logticks())
Removes specified layers from a ggplot object.
remove_geom(p, geom, idx = NULL)
remove_geom(p, geom, idx = NULL)
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 |
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
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
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.
rgg(p, oldGeom, oldGeomIdx = 1, newLayer = NULL)
rgg(p, oldGeom, oldGeomIdx = 1, newLayer = NULL)
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 |
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 }
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 }
function to tidy the ggedit output to single script calls for each plot
## S3 method for class 'ggedit' summary(object, ...)
## S3 method for class 'ggedit' summary(object, ...)
object |
ggedit object |
... |
used to pass show.structure |
use show.structure (boolean) to control if the output shows the full data.frame structure or just a placeholder [data.frame], Default: FALSE