Skip to content

Commit a35570f

Browse files
committed
clean code
1 parent cae6a7c commit a35570f

6 files changed

Lines changed: 3007 additions & 2892 deletions

File tree

R/fplot.R

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
#' Aggregate/conditional graphs and automatic layout using formulas
22
#'
3-
#' \pkg{fplot} provides automatic plotting of common graphs (distributions, lines, bar plots and boxplots). The syntax uses formulas, allowing aggregate/conditional/weighted graphs with minimum efforts. The many arguments are automatically adjusted to the data in order to provide the nicest and most meaningful graphs.
3+
#' \pkg{fplot} provides automatic plotting of common graphs (distributions, lines,
4+
#' bar plots and boxplots). The syntax uses formulas, allowing
5+
#' aggregate/conditional/weighted graphs with minimum efforts. The many arguments
6+
#' are automatically adjusted to the data in order to provide the nicest and most meaningful graphs.
47
#'
58
#' @details
69
#'
710
#' The core functions is \code{\link[fplot]{plot_distr}} to draw distributions.
8-
#' Two other graphical functions are provided for convenience: \code{\link[fplot]{plot_lines}} to represent the (usually temporal) evolution of some variables, and \code{\link[fplot]{plot_box}} to easily represent conditional boxplots.
11+
#' Two other graphical functions are provided for convenience: \code{\link[fplot]{plot_lines}}
12+
#' to represent the (usually temporal) evolution of some variables,
13+
#' and \code{\link[fplot]{plot_box}} to easily represent conditional boxplots.
914
#'
10-
#' It also integrates tools to easily export graphs: \code{\link[fplot]{pdf_fit}} and \code{\link[fplot]{png_fit}}. In these functions, instead of providing the size of the graphics, you instead give the point size that the text should have in the final document--because an exported graph usually ends up in a document. You can set the size of your document with the function \code{\link[fplot]{setFplot_page}}. If you use the function \code{\link[fplot]{fit.off}} to close the connection, you will also see how the export looks like in the Viewer pane.
15+
#' It also integrates tools to easily export graphs: \code{\link[fplot]{pdf_fit}} and
16+
#' \code{\link[fplot]{png_fit}}. In these functions, instead of providing the size of
17+
#' the graphics, you instead give the point size that the text should have in the final
18+
#' document--because an exported graph usually ends up in a document. You can set the
19+
#' size of your document with the function \code{\link[fplot]{setFplot_page}}.
20+
#' If you use the function \code{\link[fplot]{fit.off}} to close the connection,
21+
#' you will also see how the export looks like in the Viewer pane.
22+
#'
23+
#' It offers a handy tool, [`export_graph_start`], to easily export graphs with the
24+
#' objective to fit into a document. Some common graphical parameters can be directly
25+
#' modified using this function.
1126
#'
1227
#'
1328
#'

R/miscfuns.R

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,8 @@ truncate_string = function(x, trunc = 20, method = "auto"){
14321432

14331433

14341434

1435-
legendFit = function(where = "top", legend, minCex = 0.7, trunc, trunc.method = "auto", plot = TRUE, title = NULL, title_out = FALSE, ...){
1435+
legendFit = function(where = "top", legend, minCex = 0.7, trunc, trunc.method = "auto",
1436+
plot = TRUE, title = NULL, title_out = FALSE, ...){
14361437
# units in inch to avoid the need of having a graph already plotted
14371438
# (you cannot use par("usr) when there is no graph plotted)
14381439
# title_out: veut dire que le titre peut aller au dela de la plotting box
@@ -1715,7 +1716,7 @@ myBox = function(id){
17151716

17161717
}
17171718

1718-
shade_area <- function(y1, y2, x, xmin, xmax, col="grey", ...){
1719+
shade_area <- function(y1, y2, x, xmin, xmax, col = "grey", ...){
17191720
# fonction plus pratique que polygon
17201721
# elle permet de griser une partie d?limit?e par
17211722
# y1 et y2 pour chacune des valeurs de x
@@ -1743,7 +1744,9 @@ shade_area <- function(y1, y2, x, xmin, xmax, col="grey", ...){
17431744

17441745

17451746

1746-
abplot <- function(x, y, where="default", signifCode = c("***" = 0.001, "**" = 0.05, "*" = 0.10), log = FALSE, legend=TRUE, ...){
1747+
abplot = function(x, y, where = "default",
1748+
signifCode = c("***" = 0.001, "**" = 0.05, "*" = 0.10),
1749+
log = FALSE, legend = TRUE, ...){
17471750
#plot a graph with the linear fit
17481751
#where: where to place the legend
17491752

@@ -1985,7 +1988,7 @@ rightLine = function(x, y, ylim = NULL, ylim_left = NULL, showAxis = TRUE, showP
19851988
}
19861989

19871990

1988-
drawRectangle = function(xbl, ybl, xtr, ytr, prop=1, coul=1:100, sep=0.02, ...){
1991+
drawRectangle = function(xbl, ybl, xtr, ytr, prop = 1, coul = 1:100, sep = 0.02, ...){
19891992
# on donne a la fonction:
19901993
# - le (x,y) en bas a gauche du rectangle (bl: bottm left)
19911994
# - le (x,y) en haut a droite du rectangle (tr: top right)
@@ -2007,7 +2010,8 @@ drawRectangle = function(xbl, ybl, xtr, ytr, prop=1, coul=1:100, sep=0.02, ...){
20072010
}
20082011

20092012

2010-
myHist = function(x, maxValue = +Inf, cex.text = 0.7, doubleTable = FALSE, log = FALSE, use_xaxis, inCol = "#386CB0", outCol = "white", ...){
2013+
myHist = function(x, maxValue = +Inf, cex.text = 0.7, doubleTable = FALSE, log = FALSE,
2014+
use_xaxis, inCol = "#386CB0", outCol = "white", ...){
20112015
# personalized histogram
20122016

20132017
if(doubleTable){
@@ -2097,7 +2101,11 @@ myHist = function(x, maxValue = +Inf, cex.text = 0.7, doubleTable = FALSE, log =
20972101
}
20982102

20992103

2100-
myBarplot = function(x, order=FALSE, nbins=10, show0=TRUE, cex.text=0.7, isLog=FALSE, isDistribution = TRUE, yaxis.show = TRUE, niceLabels = FALSE, labels.tilted=FALSE, axis1Opts = list(), hgrid = TRUE, top = "nb", showOther = TRUE, inCol = "#386CB0", outCol = "white", trunc=20, trunc.method = "auto", line.max, ...){
2104+
myBarplot = function(x, order = FALSE, nbins = 10, show0 = TRUE, cex.text = 0.7,
2105+
isLog = FALSE, isDistribution = TRUE, yaxis.show = TRUE,
2106+
niceLabels = FALSE, labels.tilted = FALSE, axis1Opts = list(),
2107+
hgrid = TRUE, top = "nb", showOther = TRUE, inCol = "#386CB0",
2108+
outCol = "white", trunc = 20, trunc.method = "auto", line.max, ...){
21012109
# This function draws a nice barplot
21022110

21032111
# We get whether the labels from x are numeric
@@ -2322,7 +2330,8 @@ plot_line = function(x, y, addFit = FALSE, add = FALSE, smoothing_window = 0, ..
23222330

23232331

23242332

2325-
box_single = function(x, y_min, q1, med, q3, y_max, xRight, width, inCol = NA, outCol = "black", lwd = 2, lwd.med = lwd + 2, density = -1){
2333+
box_single = function(x, y_min, q1, med, q3, y_max, xRight, width, inCol = NA,
2334+
outCol = "black", lwd = 2, lwd.med = lwd + 2, density = -1){
23262335
# Optional: xRight/width: we need one of the two!
23272336
# either it is x, xRight, meaning that the rect x-dimension will be xLeft and xRight
23282337
# either it is x, width, meaning that the rect x-dimension will be x-width/2 and x+width/2
@@ -2393,12 +2402,14 @@ box_single = function(x, y_min, q1, med, q3, y_max, xRight, width, inCol = NA, o
23932402

23942403
}
23952404

2396-
abplot <- function(x, y, where="default", signifCode = c("***" = 0.001, "**" = 0.05, "*" = 0.10), log = FALSE, legend=TRUE, ...){
2397-
#plot a graph with the linear fit
2398-
#where: where to place the legend
2405+
abplot = function(x, y, where="default",
2406+
signifCode = c("***" = 0.001, "**" = 0.05, "*" = 0.10),
2407+
log = FALSE, legend = TRUE, ...){
2408+
# plot a graph with the linear fit
2409+
# where: where to place the legend
23992410

24002411
# we take care of xlabs
2401-
dots <- list(...)
2412+
dots = list(...)
24022413
if(is.null(dots$xlab)){
24032414
dots$xlab = deparse(substitute(x))
24042415
if(log) dots$xlab = paste0("ln(", dots$xlab, ")")
@@ -2511,7 +2522,11 @@ find_margins_left = function(ylab, y_labels, ylab.resize){
25112522
list(ylab = ylab, ylab.line = ylab.line, total_width = total_width)
25122523
}
25132524

2514-
find_margins_bottom = function(xlab, sub, data_freq, log, isNum, numLabel, numAxis, nbins, DO_SPLIT, ADD_OTHER, ADD_OTHER_LEFT, sorted, labels.tilted, delayLabelsTilted, checkForTilting, checkNotTilted, noSub, binned_data, line.max, trunc, trunc.method, cex.axis, labels.angle, at_5, xlim){
2525+
find_margins_bottom = function(xlab, sub, data_freq, log, isNum, numLabel, numAxis,
2526+
nbins, DO_SPLIT, ADD_OTHER, ADD_OTHER_LEFT, sorted,
2527+
labels.tilted, delayLabelsTilted, checkForTilting,
2528+
checkNotTilted, noSub, binned_data, line.max, trunc,
2529+
trunc.method, cex.axis, labels.angle, at_5, xlim){
25152530
# This function finds the size of the margin needed to display all the x-axis labels + xlab + sub
25162531
# This is highly complex because the decision on how to show the x-axis labels
25172532
# depend on many things in plot_distr.
@@ -3288,7 +3303,12 @@ isVector = function(x){
32883303
####
32893304

32903305

3291-
plot_bar = function(fml, data, agg, fun = mean, dict = getFplot_dict(), order=FALSE, nbins=50, show0=TRUE, cex.text=0.7, isDistribution = FALSE, yaxis.show = TRUE, labels.tilted, trunc = 20, trunc.method = "auto", line.max, hgrid = TRUE, top = "nb", showOther = TRUE, inCol = "#386CB0", border = "white", xlab, ylab, ...){
3306+
plot_bar = function(fml, data, agg, fun = mean, dict = getFplot_dict(),
3307+
order = FALSE, nbins = 50, show0 = TRUE, cex.text = 0.7,
3308+
isDistribution = FALSE, yaxis.show = TRUE, labels.tilted,
3309+
trunc = 20, trunc.method = "auto", line.max, hgrid = TRUE,
3310+
top = "nb", showOther = TRUE, inCol = "#386CB0",
3311+
border = "white", xlab, ylab, ...){
32923312
# this function formats a bit the data and sends it to myBarplot
32933313

32943314
# Old params
@@ -3451,7 +3471,12 @@ plot_bar = function(fml, data, agg, fun = mean, dict = getFplot_dict(), order=FA
34513471
ylab = x_name
34523472
}
34533473

3454-
myBarplot(x = res, order=order, nbins=nbins, show0=show0, cex.text=cex.text, isLog=isLog, isDistribution = isDistribution, yaxis.show = yaxis.show, niceLabels = TRUE, labels.tilted=labels.tilted, trunc = trunc, trunc.method = trunc.method, line.max=line.max, hgrid = hgrid, top = top, showOther = showOther, inCol = inCol, outCol = border, xlab = xlab, ylab = ylab, ...)
3474+
myBarplot(x = res, order = order, nbins = nbins, show0 = show0, cex.text = cex.text,
3475+
isLog = isLog, isDistribution = isDistribution, yaxis.show = yaxis.show,
3476+
niceLabels = TRUE, labels.tilted = labels.tilted, trunc = trunc,
3477+
trunc.method = trunc.method, line.max = line.max, hgrid = hgrid,
3478+
top = top, showOther = showOther, inCol = inCol, outCol = border,
3479+
xlab = xlab, ylab = ylab, ...)
34553480

34563481
invisible(base_agg)
34573482
}

0 commit comments

Comments
 (0)