Title: | Compute a Non-Overlapping Layout of Text Boxes to Label Multiple Overlain Plots |
---|---|
Description: | Compute a non-overlapping layout of text boxes to label multiple overlain curves. For each curve, iteratively search for an adjacent x,y position for the text box that does not overlap with the other curves. If this process fails, then offsets are computed to add to the y values for each curve, that results in sufficient space to add all of the text labels. |
Authors: | Barry Zeeberg [aut, cre] |
Maintainer: | Barry Zeeberg <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.0 |
Built: | 2024-10-31 20:27:37 UTC |
Source: | https://github.com/cran/textBoxPlacement |
compute correct axes ranges for a set of overlain plots
axesRange(xList)
axesRange(xList)
xList |
list of numerical vectors containing axis points |
xlim
axesRange(yList1)
axesRange(yList1)
compute the indices of sorting yrange in decreasing order
permInd(yrange)
permInd(yrange)
yrange |
numeric vector (max-min) for vector of y values |
returns the integer vector indices of sorting yrange in decreasing order
permInd(yrange(yList1))
permInd(yrange(yList1))
reorder multiple curves so that the curve with the largest y range is on the bottom of the staggered graph
permuteCurves(xList, yList, tList, textBoxHeights, permInd)
permuteCurves(xList, yList, tList, textBoxHeights, permInd)
xList |
list whose components are numeric vectors of the x values for overlaid curves |
yList |
list whose components are numeric vectors of the y values for overlaid curves |
tList |
list of character string texts to insert in plot |
textBoxHeights |
return value of textBoxUserUnits() |
permInd |
return value of permInd() |
returns a list whose (re-ordered) components are:
xList a list of numeric vector for x values
yList a list of numeric vector for y values - re-ordered and offset-adjusted
tList a list of character strings for text boxes to label the curves
offset a numeric vector offset to add to each staggered curve
ystart a numeric vector of starting positions
units<-textBoxUserUnits(textList,yrange(yList1),verbose=TRUE) permuteCurves(xList1,yList1,textList,units,permInd(yrange(yList1)))
units<-textBoxUserUnits(textList,yrange(yList1),verbose=TRUE) permuteCurves(xList1,yList1,textList,units,permInd(yrange(yList1)))
compute x and y coordinates for placement of text box based upon the values of the function to avoid running into the graph line
positionTextBox(text, x, y, xPos, adj, nApprox = 10, reallyText)
positionTextBox(text, x, y, xPos, adj, nApprox = 10, reallyText)
text |
character string text to insert in plot |
x |
numeric vector of x values |
y |
numeric vector of y values |
xPos |
numeric x position for text box |
adj |
numeric vector param passed to text() |
nApprox |
integer number of intervals to interpolate between x data points |
reallyText |
Boolean if TRUE then execute text() command |
returns a list c(ymin,ymax,strheight(text),xPos1)
x<-1:10 y<-1:10 plot(x,y,type="l") positionTextBox(text="TEXT ME",x=x,y=y,xPos=1, adj=c(0,0),nApprox=10,reallyText=TRUE)
x<-1:10 y<-1:10 plot(x,y,type="l") positionTextBox(text="TEXT ME",x=x,y=y,xPos=1, adj=c(0,0),nApprox=10,reallyText=TRUE)
Driver to compute x and y coordinates for placement of text box based upon the values of the function to avoid running into the graph line and avoid overlapping with other overlay curves
positionTextBoxDriver( text, x, y, xPos, nApprox = 10, xList, yList, stag = FALSE, offset = 0, verbose )
positionTextBoxDriver( text, x, y, xPos, nApprox = 10, xList, yList, stag = FALSE, offset = 0, verbose )
text |
character string text to insert in plot |
x |
numeric vector of x values |
y |
numeric vector of y values |
xPos |
numeric x position for text box |
nApprox |
integer number of intervals to interpolate between x data points |
xList |
list whose components are numeric vectors of the x values for overlaid curves |
yList |
list whose components are numeric vectors of the y values for overlaid curves |
stag |
Boolean TRUE if this plot has staggering added to curves |
offset |
numeric vector of offsets added to each curve |
verbose |
Boolean if TRUE print informative or diagnostic messages to console |
returns a numeric vector c(yReal,yAdj) where xTry is an x value at which the text box will fit without overlapping another overlay curve, or returns -1000000 for failure
positionTextBoxDriver(text="TEXT ME",x=xList1[[1]],y=yList1[[1]], xPos=1,nApprox=10,xList=xList1[-1],yList=yList1[-1],stag=FALSE,offset=0,verbose=TRUE)
positionTextBoxDriver(text="TEXT ME",x=xList1[[1]],y=yList1[[1]], xPos=1,nApprox=10,xList=xList1[-1],yList=yList1[-1],stag=FALSE,offset=0,verbose=TRUE)
Driver to compute x and y coordinates for placement of text box based upon the y values of the function to avoid running into the graph line and avoid overlapping with other overlay curves
positionTextBoxDriverDriver( xList, yList, textList, xPos, nApprox = 10, labs, stag = FALSE, offset = 0, ystart, ylim, verbose )
positionTextBoxDriverDriver( xList, yList, textList, xPos, nApprox = 10, labs, stag = FALSE, offset = 0, ystart, ylim, verbose )
xList |
list whose components are numeric vectors of the x values for overlaid curves |
yList |
list whose components are numeric vectors of the y values for overlaid curves |
textList |
list of character string texts to insert in plot |
xPos |
numeric vector x position for text box |
nApprox |
integer number of intervals to interpolate between x data points |
labs |
list of labels annotating a graph
|
stag |
Boolean TRUE if this plot has staggering added to curves |
offset |
numeric vector of offsets added to each curve |
ystart |
numeric vector of starting positions |
ylim |
numeric vector ylim parameter for plot() |
verbose |
Boolean if TRUE print informative or diagnostic messages to console |
if the length of the return value is not 0, then additional processing might be needed for the bad curves, such as adding an offset to their y values, plotting them in a different color or symbol, and keying them to a second y axis on the right of the graph
returns a vector of integers indicating curves whose text box could not be drawn
# the text box for the second curve cannot fit, # as it is sandwiched between two curves that are too close plot(xList1[[1]],yList1[[1]],type="l") positionTextBoxDriverDriver(xList=xList1,yList=yList1, textList=textList,xPos=c(1,1,1),nApprox=10,labs=labs, stag=FALSE,offset=0,ystart=0,ylim=axesRange(yList1),verbose=TRUE)
# the text box for the second curve cannot fit, # as it is sandwiched between two curves that are too close plot(xList1[[1]],yList1[[1]],type="l") positionTextBoxDriverDriver(xList=xList1,yList=yList1, textList=textList,xPos=c(1,1,1),nApprox=10,labs=labs, stag=FALSE,offset=0,ystart=0,ylim=axesRange(yList1),verbose=TRUE)
Driver invoke positionTextBoxDriverDriver() using the original user input data If that fails to produce an uncluttered plot, then invokes stagger() to reshape the data before re-running positionTextBoxDriverDriver().
positionTextBoxDriverDriverDriver( xList, yList, textList, xPos, nApprox = 10, labs, sortB, verbose = FALSE )
positionTextBoxDriverDriverDriver( xList, yList, textList, xPos, nApprox = 10, labs, sortB, verbose = FALSE )
xList |
(optional) list whose components are numeric vectors of the x values for overlaid curves |
yList |
list whose components are numeric vectors of the y values for overlaid curves |
textList |
(optional) list of character string texts to insert in plot |
xPos |
(optional) numeric vector x position for text box |
nApprox |
(optional) integer number of intervals to interpolate between x data points |
labs |
(optional) list of labels annotating a graph
|
sortB |
Boolean if TRUE staggered curves reordered, with largest range curve on bottom of graph |
verbose |
Boolean if TRUE print informative or diagnostic messages to console |
if xList or textList is missing, it is constructed from elements in yList
hint: to prevent conflicts, run the following line manually before running positionTextBoxDriverDriverDriver()
rm(list=ls())
see https://stackoverflow.com/questions/27253376/different-results-from-rscript-and-r-cmd-batch
returns no values, but has side effect of generating a graph.
# There is not enough space for text boxes in original graph. # The package automatically adds offsets to the curves, # keeping the curves in the original order, # and successfully adds text boxes positionTextBoxDriverDriverDriver(xList=xList1,yList=yList1, textList=textList,xPos=c(1,1,1),nApprox=10,labs=labs,sortB=FALSE) # data set contains some negative values positionTextBoxDriverDriverDriver(xList=xList2,yList=yList2, textList=textList,xPos=c(1,1,1),nApprox=10,labs=labs,sortB=FALSE) # show the difference when we sort the order of the curves # to position the curve with the largest range on the bottom positionTextBoxDriverDriverDriver(xList=xList2,yList=yList2, textList=textList,xPos=c(1,1,1),nApprox=10,labs=labs,sortB=TRUE,verbose=TRUE)
# There is not enough space for text boxes in original graph. # The package automatically adds offsets to the curves, # keeping the curves in the original order, # and successfully adds text boxes positionTextBoxDriverDriverDriver(xList=xList1,yList=yList1, textList=textList,xPos=c(1,1,1),nApprox=10,labs=labs,sortB=FALSE) # data set contains some negative values positionTextBoxDriverDriverDriver(xList=xList2,yList=yList2, textList=textList,xPos=c(1,1,1),nApprox=10,labs=labs,sortB=FALSE) # show the difference when we sort the order of the curves # to position the curve with the largest range on the bottom positionTextBoxDriverDriverDriver(xList=xList2,yList=yList2, textList=textList,xPos=c(1,1,1),nApprox=10,labs=labs,sortB=TRUE,verbose=TRUE)
unclutter the overlay plot by staggering the graphs
stagger(xList, yList, tList, sortB = FALSE, verbose)
stagger(xList, yList, tList, sortB = FALSE, verbose)
xList |
list whose components are numeric vectors of the x values for overlaid curves |
yList |
list whose components are numeric vectors of the y values for overlaid curves |
tList |
list of character string texts to insert in plot |
sortB |
Boolean if TRUE staggered curves reordered, with largest range curve on bottom of graph |
verbose |
Boolean if TRUE print informative or diagnostic messages to console |
In order to unclutter the overlay plot, we need to stagger the graphs the offset for each graph will be the sum of the max values for all of the preceding graphs. So the stack of staggered graphs will have max y (ie, ymax) equal to the sum of the max's.
returns a list whose components are:
textBoxHeights return value of textBoxUserUnits()
permInd return value of permInd()
xlim numeric vector parameter for plot()
ylim numeric vector parameter for plot()
# demonstrate effect of sorting the curves plot.new() stagger(xList2,yList2,textList,sortB=FALSE,verbose=TRUE) stagger(xList2,yList2,textList,sortB=TRUE,verbose=TRUE)
# demonstrate effect of sorting the curves plot.new() stagger(xList2,yList2,textList,sortB=FALSE,verbose=TRUE) stagger(xList2,yList2,textList,sortB=TRUE,verbose=TRUE)
compute the heights of the text boxes in user units
textBoxUserUnits(tList, yrange, verbose)
textBoxUserUnits(tList, yrange, verbose)
tList |
a list of character strings for text boxes to label the curves |
yrange |
numeric vector (max-min) for vector of y values |
verbose |
Boolean if TRUE print informative or diagnostic messages to console |
numeric vector of the heights of the text boxes in user units
textBoxUserUnits(textList,yrange(yList1),verbose=TRUE)
textBoxUserUnits(textList,yrange(yList1),verbose=TRUE)
textBoxPlacement data sets
data(textList)
data(textList)
compute the numeric vector xlim for a set of curves
xlim(xList)
xlim(xList)
xList |
list whose components are numeric vectors of the x values for overlaid curves |
numeric vector xlim
xlim(xList1)
xlim(xList1)
is the value of xPos within a valid range?
xPosCheck(xPos, xList, verbose)
xPosCheck(xPos, xList, verbose)
xPos |
integer specifying x position to try to place text box |
xList |
list whose components are numeric vectors of the x values for overlaid curves |
verbose |
Boolean if TRUE print informative or diagnostic messages to console |
numeric vector valid values of xPos
# replace incorrect xPos with reasonable value xPosCheck(c(1,1,-5),xList2,verbose=TRUE)
# replace incorrect xPos with reasonable value xPosCheck(c(1,1,-5),xList2,verbose=TRUE)
compute the numeric vector ylim
ylim(yList, yrange, textBoxHeights, sortB, permInd)
ylim(yList, yrange, textBoxHeights, sortB, permInd)
yList |
list whose components are numeric vectors of the y values for overlaid curves |
yrange |
numeric vector (max-min) for vector of y values |
textBoxHeights |
return value of textBoxUserUnits() |
sortB |
Boolean if TRUE staggered curves are reordered, with largest range curve on bottom of graph |
permInd |
return value of permInd() |
returns a numeric vector ylim
# demonstrate effect of sorting the curves plot.new() ylim(yList1,yrange(yList1),textBoxUserUnits(textList,yrange(yList1),verbose=TRUE), FALSE,permInd(yrange(yList1)))
# demonstrate effect of sorting the curves plot.new() ylim(yList1,yrange(yList1),textBoxUserUnits(textList,yrange(yList1),verbose=TRUE), FALSE,permInd(yrange(yList1)))
compute the staggered y values for the overlay plot
yrange(yList)
yrange(yList)
yList |
list whose components are numeric vectors of the y values for overlaid curves |
numeric vector yrange (max-min) for vector of y values
yrange(yList1)
yrange(yList1)