Run a Positioning Method list on a given data set. This function contains all the logic for parsing a Positioning Method and sequentially applying its elements to the input data to obtain the label positions.
apply.method <- function # Apply a Positioning Method ### Run a Positioning Method list on a given data set. This function ### contains all the logic for parsing a Positioning Method and ### sequentially applying its elements to the input data to obtain the ### label positions. (method, ### Direct labeling Positioning Method. Starting from the data frame ### of points to plot for the panel, the elements of the Positioning ### Method list are applied in sequence, and then each row of the ### resulting data frame is used to draw a direct label. The ### elements of a Positioning Method list can be ### itemize{ ### item a Positioning Function is any function(d,...) which takes a ### data.frame d with columns x,y,groups and returns another ### data.frame representing the positions of the desired direct ### labels. For a description of all the columns that are interpreted ### for drawing direct labels, see code{link{drawDetails.dlgrob}}. ### For example, maxvar.points is a Positioning Function that returns ### a data.frame with columns x,y,groups,hjust,vjust. ### item a character vector of length 1 is treated as the name of an ### R object. For example, specifying "maxvar.points" means to look up ### the variable called maxvar.points and use that. Using the name of ### a Positioning Function is preferable to specifying the Positioning ### Function itself, since then the name is visible in the Positioning ### Method list, which is more interpretable when debugging. ### item a named list element is used to add or update variables in ### the data.frame of direct labels to plot. For example ### list("first.points",cex=1.5) means take only the first points of ### every group and then set the cex column to 1.5. ### item an element of a Positioning Method list can be another ### Positioning Method list, in which case the elements of the inner ### list are applied. ### } d, ### Data frame to which we apply the Positioning Method. The x and y ### columns should be in centimeters (cm), so that Positioning Methods ### can easily calculate the L2/Euclidean/visual distance between ### pairs of points. columns.to.check=c("x","y","groups"), ### After applying each Positioning Method list element, we check for ### the presence of these columns, and if not found we stop with an ### error. ..., ### Named arguments, passed to Positioning Functions. debug=FALSE ### If TRUE, print each Positioning Method list elmenent and the ### direct label data.frame that results from its evaluation. ){ attr(d,"orig.data") <- d ##DONT DELETE: if the first Positioning ##Method needs orig.data, this needs to be ##here! check.for.columns(d,columns.to.check) if(!is.list(method))method <- list(method) isconst <- function(){ m.var <- names(method)[1] !(is.null(m.var)||m.var=="") } islist <- function()is.list(method[[1]]) isref <- function()(!isconst())&&is.character(method[[1]]) while(length(method)){ if(debug)print(method[1])##not [[1]] --- named items! ##browser() ## Resolve any names or nested lists while(islist()||isref()){ if(islist()){ method <- c(method[[1]],method[-1]) }else{ #must be character -> get the fun(s) if(length(method[[1]])>1){ warning("using first element of character vector") method[[1]] <- method[[1]][1] } method <- c(get(method[[1]]),method[-1]) } } if(isconst()) d[[names(method)[1]]] <- method[[1]] else{ #should be a Positioning Function old <- d group.dfs <- split(d,d$groups) group.specific <- lapply(group.dfs,only.unique.vals) to.restore <- Reduce(intersect,lapply(group.specific,names)) d <- method[[1]](d,debug=debug,...) check.for.columns(d,columns.to.check) ## do not restore if they are present in the returned list! to.restore <- to.restore[!to.restore %in% names(d)] for(N in to.restore){ d[[N]] <- NA for(g in unique(d$groups)){ d[d$groups==g,N] <- group.specific[[g]][,N] } } attr(d,"orig.data") <- if(is.null(attr(old,"orig.data")))old else attr(old,"orig.data") } if(debug){ print(d) } method <- method[-1] } d ### The final data frame returned after applying all of the items in ### the Positioning Method list, with x and y in units of cm. }
Please contact Toby Dylan Hocking if you are using directlabels or have ideas to contribute, thanks! |
Documentation website generated from source code version 2021.2.24 (git revision bb6db07 Mon, 14 Jun 2021 22:38:45 +0530) using inlinedocs. |
validate |