@@ -23,27 +23,22 @@ add_to_legend <- function(object, fun.name, legend.name, call.args, option.args)
2323 if (is.null(legend.name )) {
2424 return (object )
2525 }
26-
27-
28- # add legend$gs.config if it does not already exist
29- legend.exists <- " legend" %in% names(object )
30- legend.args.exist <- " legend.args" %in% names(object [[' legend' ]])
31- if (! legend.exists ){object <- modify_legend(object )}
32-
33- # add/add to legend$legend.args
26+ if (! exists(" legend" , object )) {object $ legend <- list ()}
27+ if (! exists(" legend.auto" , object $ legend )) {object $ legend $ legend.auto <- create_empty_legend()}
28+
29+ # add/add to legend$legend.auto
3430 if (length(legend.name ) > 1 ){
3531 call.args.df <- as.data.frame(call.args , stringsAsFactors = FALSE )
3632
3733 for (p in seq(nrow(call.args.df ))) {
38- if (p > 1 ){ legend.args.exist <- TRUE } # legend args will exist after the first loop
3934 call.args.list <- as.list(call.args.df [p ,])
4035 fun.legend.args <- get_legend_args(fun.name , call.args.list , legend.name [p ], option.args )
41- object [[' legend' ]][[' legend.args ' ]] <- combine_legend_args(object , fun.legend.args , legend.args.exist )
36+ object [[' legend' ]][[' legend.auto ' ]] <- combine_legend_args(object , fun.legend.args )
4237 }
4338
4439 } else {
4540 fun.legend.args <- get_legend_args(fun.name , call.args , legend.name , option.args )
46- object [[' legend' ]][[' legend.args ' ]] <- combine_legend_args(object , fun.legend.args , legend.args.exist )
41+ object [[' legend' ]][[' legend.auto ' ]] <- combine_legend_args(object , fun.legend.args )
4742 }
4843
4944 return (object )
@@ -85,7 +80,7 @@ get_legend_args <- function(fun.name, call.args, legend.name, option.args){
8580 call.args <- set_type_params(call.args , type.name , params.needed )
8681 if (type.name %in% c(' p' , ' lchsS' )) {fun.name <- switch (type.name , p = " points" , lchsS = " lines" )}
8782 }
88-
83+ fun.specific <- list ()
8984 if (fun.name == " points" ) {
9085 pt.names <- c(" lwd" ," bg" ," cex" )
9186 names(call.args ) <- replace(names(call.args ), which(names(call.args ) %in% pt.names ),
@@ -145,46 +140,61 @@ set_type_params <- function(list, type.name, params){
145140# ' @param legend.args.exist
146141# ' @param .dots lazy_dots
147142# ' @keywords internal
148- combine_legend_args <- function (object , new.legend.args , legend.args.exist , ... ){
143+ combine_legend_args <- function (object , new.legend.args , ... ){
149144
150- # look for existing list of legend args in the object
151- if (! legend.args.exist ){
152- default.args <- formals(graphics :: legend )
153- overall.legend <- c(" x" , " y" , " bty" , " bg" , " box.lty" , " box.lwd" , " box.col" , " cex" ,
154- " xjust" , " yjust" , " x.intersp" , " y.intersp" , " adj" , " text.width" ,
155- " merge" , " trace" , " plot" , " ncol" , " horiz" , " title" , " inset" ,
156- " xpd" , " title.col" , " title.adj" , " seg.len" )
157- not.overall <- default.args [which(! names(default.args ) %in% overall.legend )]
158- legend.args <- vector(" list" , length(not.overall ))
159- names(legend.args ) <- names(not.overall )
160- # // set up config legend stuff
161- } else {
162- legend.args <- object [[' legend' ]][[' legend.args' ]]
163- }
145+ legend.args <- object [[' legend' ]][[' legend.auto' ]]
164146
165- orderedParams <- new.legend.args [match(names(legend.args ), names(new.legend.args ))]
147+ orderedParams <- new.legend.args [match(names(legend.args ), names(new.legend.args ))]
166148 for (j in seq_along(legend.args )) {
167149 legend.args [[j ]] <- c(legend.args [[j ]], orderedParams [[j ]])
168150 }
169151
170152 return (legend.args )
171153}
172154
155+ # ' Set up an empty legend
156+ # '
157+ create_empty_legend <- function () {
158+ default.args <- formals(graphics :: legend )
159+ overall.legend <- c(" x" , " y" , " bty" , " bg" , " box.lty" , " box.lwd" , " box.col" , " cex" ,
160+ " xjust" , " yjust" , " x.intersp" , " y.intersp" , " adj" , " text.width" ,
161+ " merge" , " trace" , " plot" , " ncol" , " horiz" , " title" , " inset" ,
162+ " xpd" , " title.col" , " title.adj" , " seg.len" )
163+ not.overall <- default.args [which(! names(default.args ) %in% overall.legend )]
164+ legend <- vector(" list" , length(not.overall ))
165+ names(legend ) <- names(not.overall )
166+
167+ # add draw = FALSE as default
168+ legend $ draw <- FALSE
169+
170+ return (legend )
171+ }
172+
173173# ' add legend configs
174174# '
175175# ' @param object
176176# ' @keywords internal
177177modify_legend <- function (object , location = " topright" , legend_offset = 0.3 , draw = FALSE , ... ){
178178 # // this should be shared between add_to_legend and legend
179179 # // check if legend exists, if not add it (someone could call legend before any legend.names)
180- gsConfig <- list (location = location , legend_offset = legend_offset , draw = draw , ... )
181-
180+ legend.config <- list (location = location , legend_offset = legend_offset , draw = draw , ... )
181+
182182 arguments <- list (... )
183- if (" x" %in% names(arguments )){
184- gsConfig $ location <- gsConfig $ x
185- gsConfig $ x <- NULL
186- }
183+ # auto is used when "legend" arg comes from "legend.name" in gsplot calls
184+ legend.index <- ifelse(" legend" %in% names(legend.config ),length(grep(" legend.\\ d+" , names(object $ legend ))) + 1 , " auto" )
187185
188- object [[' legend' ]][[' gs.config' ]] <- gsConfig
186+ if (" x" %in% names(arguments )){
187+ legend.config $ location <- legend.config $ x
188+ legend.config $ x <- NULL
189+ }
190+ if (legend.index == ' auto' ) {
191+ # Merge new legend config into existing auto legend if it exists
192+ auto.legend <- object $ legend $ legend.auto
193+ # if draw is true, stay true
194+ legend.config $ draw <- legend.config $ draw || is.null(auto.legend ) || auto.legend $ draw
195+ auto.legend [names(legend.config )] <- legend.config
196+ legend.config <- auto.legend
197+ }
198+ object [[' legend' ]][[paste0(" legend." , legend.index )]] <- legend.config
189199 return (object )
190200}
0 commit comments