Title: | Karl Broman's R Code |
---|---|
Description: | Miscellaneous R functions, including functions related to graphics (mostly for base graphics), permutation tests, running mean/median, and general utilities. |
Authors: | Karl W Broman [aut, cre]
|
Maintainer: | Karl W Broman <[email protected]> |
License: | GPL-3 |
Version: | 0.84 |
Built: | 2024-08-16 05:01:50 UTC |
Source: | https://github.com/kbroman/broman |
%in%
returns logical vector indicating values that do not have a match.
%win%
returns a vector of the values that have a match.
%wnin%
returns a vector of the values that do not have a match.
x %nin% table x %win% table x %wnin% table
x %nin% table x %win% table x %wnin% table
x |
Vector of values to be matched. |
table |
Vector of values to be matched against. |
%nin%
returns a logical vector of the same length of x
, indicating which values are not in table
.
%win%
returns a sub-vector of x
with the values that were found in table
.
%wnin%
returns a sub-vector of x
with the values that were not found in table
.
vals <- c("a", "xa", "b") vals %nin% letters vals %win% letters vals %wnin% letters
vals <- c("a", "xa", "b") vals %nin% letters vals %win% letters vals %wnin% letters
Convert a number to a string, with commas every 3rd digit
add_commas(numbers)
add_commas(numbers)
numbers |
Vector of non-negative numbers (will be rounded to integers) |
Character string with numbers written like "7,547,085"
.
add_commas(c(231, 91310, 2123, 9911001020, 999723285))
add_commas(c(231, 91310, 2123, 9911001020, 999723285))
Align two vectors using their names attributes, either expanding with NAs or reducing to the common values.
align_vectors(x, y, expand = TRUE)
align_vectors(x, y, expand = TRUE)
x |
A vector |
y |
Another vector |
expand |
If TRUE, expand each to the same length using NAs. If FALSE, remove elements not in common. |
A list with two components, x
and y
Use the graphics::locator()
function to indicate the
endpoints of an arrow and then plot it.
arrowlocator( reverse = FALSE, horizontal = FALSE, vertical = FALSE, length = 0.1, ... )
arrowlocator( reverse = FALSE, horizontal = FALSE, vertical = FALSE, length = 0.1, ... )
reverse |
If FALSE, first indicate the tail of the arrow and then the head; if TRUE, first indicate the head of the arrow and then the tail. |
horizontal |
If TRUE, force the arrow to be horizontal. (Use the average y-axis value of the two clicks for the vertical placement.) |
vertical |
If TRUE, force the arrow to be vertical. (Use the average x-axis value of the two clicks for the horizontal placement.) |
length |
Length of the edges of the arrow head. |
... |
Additional graphics parameters |
Use graphics::locator()
to indicate the two endpoints of
an arrow and then draw it.
The locations of the endpoints of the arrow, as a two-row matrix. The first row indicates the location of the tail of the arrow; the second row indicates the location of the head of the arrow.
graphics::arrows()
, graphics::locator()
## Not run: plot(0,0,type="n", xlab="", ylab="", xlim=c(0,100), ylim=c(0,100)) arrowlocator(col="blue", lwd=2) ## End(Not run)
## Not run: plot(0,0,type="n", xlab="", ylab="", xlim=c(0,100), ylim=c(0,100)) arrowlocator(col="blue", lwd=2) ## End(Not run)
Get the names of the attributes of an object
attrnames(object)
attrnames(object)
object |
Any object |
It just does names(attributes(object))
.
Vector of character strings with the names of the attributes.
x <- matrix(1:100, ncol=5) colnames(x) <- LETTERS[1:5] attrnames(x)
x <- matrix(1:100, ncol=5) colnames(x) <- LETTERS[1:5] attrnames(x)
Creates different vectors of related colors that may be useful for figures.
brocolors( set = c("general", "general2", "bg", "bgpng", "CC", "CCalt", "f2", "sex", "main", "crayons", "web") )
brocolors( set = c("general", "general2", "bg", "bgpng", "CC", "CCalt", "f2", "sex", "main", "crayons", "web") )
set |
Character string indicating a set of colors. |
Vector of character strings representing the chosen set of colors, in RGB.
par(mar=c(0.6,5.1,0.6,0.6)) plot(0, 0, type="n", xlab="", ylab="", xlim=c(0, 9), ylim=c(8.5, 0), yaxs="i", xaxt="n", yaxt="n", xaxs="i") axis(side=2, at=1:8, c("general", "general2", "bg", "bgpng", "CC", "f2", "sex", "main"), las=1) gen <- brocolors("general") points(seq(along=gen), rep(1,length(gen)), pch=21, bg=gen, cex=4) text(seq(along=gen), rep(c(0.55, 0.7), length(gen))[seq(along=gen)], names(gen)) gen2 <- brocolors("general2") points(seq(along=gen2), rep(2,length(gen2)), pch=21, bg=gen2, cex=4) text(seq(along=gen2), rep(1+c(0.55, 0.7), length(gen2))[seq(along=gen2)], names(gen2)) points(1, 3, pch=21, bg=brocolors("bg"), cex=4) points(1, 4, pch=21, bg=brocolors("bgpng"), cex=4) CC <- brocolors("CC") points(seq(along=CC), rep(5,length(CC)), pch=21, bg=CC, cex=4) text(seq(along=CC), rep(4+c(0.55, 0.7), length(CC))[seq(along=CC)], names(CC)) f2 <- brocolors("f2") points(seq(along=f2), rep(6,length(f2)), pch=21, bg=f2, cex=4) text(seq(along=f2), rep(5.7, length(f2)), names(f2)) sex <- brocolors("sex") points(seq(along=sex), rep(7,length(sex)), pch=21, bg=sex, cex=4) text(seq(along=sex), rep(6.7, length(sex)), names(sex)) points(1, 8, pch=21, bg=brocolors("main"), cex=4)
par(mar=c(0.6,5.1,0.6,0.6)) plot(0, 0, type="n", xlab="", ylab="", xlim=c(0, 9), ylim=c(8.5, 0), yaxs="i", xaxt="n", yaxt="n", xaxs="i") axis(side=2, at=1:8, c("general", "general2", "bg", "bgpng", "CC", "f2", "sex", "main"), las=1) gen <- brocolors("general") points(seq(along=gen), rep(1,length(gen)), pch=21, bg=gen, cex=4) text(seq(along=gen), rep(c(0.55, 0.7), length(gen))[seq(along=gen)], names(gen)) gen2 <- brocolors("general2") points(seq(along=gen2), rep(2,length(gen2)), pch=21, bg=gen2, cex=4) text(seq(along=gen2), rep(1+c(0.55, 0.7), length(gen2))[seq(along=gen2)], names(gen2)) points(1, 3, pch=21, bg=brocolors("bg"), cex=4) points(1, 4, pch=21, bg=brocolors("bgpng"), cex=4) CC <- brocolors("CC") points(seq(along=CC), rep(5,length(CC)), pch=21, bg=CC, cex=4) text(seq(along=CC), rep(4+c(0.55, 0.7), length(CC))[seq(along=CC)], names(CC)) f2 <- brocolors("f2") points(seq(along=f2), rep(6,length(f2)), pch=21, bg=f2, cex=4) text(seq(along=f2), rep(5.7, length(f2)), names(f2)) sex <- brocolors("sex") points(seq(along=sex), rep(7,length(sex)), pch=21, bg=sex, cex=4) text(seq(along=sex), rep(6.7, length(sex)), names(sex)) points(1, 8, pch=21, bg=brocolors("main"), cex=4)
Print the version number of the currently installed version of R/broman.
bromanversion()
bromanversion()
A character string with the version number of the currently installed version of R/broman.
bromanversion()
bromanversion()
Check whether two objects are the same, including their patterns of NA
s.
cf(a, b)
cf(a, b)
a |
Some object. |
b |
Another object |
It's not very complicated: ((is.na(a) & is.na(b)) | (!is.na(a) & !is.na(b) & a == b))
Boolean object with TRUE
indicating an element is the same.
x <- c(5, 8, 9, NA, 3, NA) y <- c(5, 2, 9, 4, NA, NA) cf(x,y) x <- matrix(rnorm(1000), ncol=20) x[sample(seq(along=x), 100)] <- NA all(cf(x,x)) dim(cf(x,x)) y <- x y[4,8] <- NA sum(!cf(x,y)) y[6,2] <- 18 sum(!cf(x,y)) y[6,5] <- 32 sum(!cf(x,y)) x <- as.data.frame(x) y <- as.data.frame(y) sum(!cf(x,y)) x <- as.list(x) y <- as.list(y) sapply(cf(x,y), function(a) sum(!a))
x <- c(5, 8, 9, NA, 3, NA) y <- c(5, 2, 9, 4, NA, NA) cf(x,y) x <- matrix(rnorm(1000), ncol=20) x[sample(seq(along=x), 100)] <- NA all(cf(x,x)) dim(cf(x,x)) y <- x y[4,8] <- NA sum(!cf(x,y)) y[6,2] <- 18 sum(!cf(x,y)) y[6,5] <- 32 sum(!cf(x,y)) x <- as.data.frame(x) y <- as.data.frame(y) sum(!cf(x,y)) x <- as.list(x) y <- as.list(y) sapply(cf(x,y), function(a) sum(!a))
Calculate a p-value for a chi-square test by Monte Carlo simulation.
chisq(tab, n.sim = 1000)
chisq(tab, n.sim = 1000)
tab |
A matrix of counts. |
n.sim |
Number of samples of permuted tables to consider. |
This is like the function stats::chisq.test()
, but
calculates an approximate P-value rather than refering to
asymptotics. This will be better for large, sparse tables.
A single number: the P-value testing independence of rows and columns in the table.
stats::chisq.test()
, stats::fisher.test()
, fisher()
TeaTasting <- matrix(c(3,1,1,3),nrow=2) chisq(TeaTasting,1000)
TeaTasting <- matrix(c(3,1,1,3),nrow=2) chisq(TeaTasting,1000)
Uses grayplot()
to plot a set of confidence intervals.
ciplot( est, se = NULL, lo = NULL, hi = NULL, SEmult = 2, labels = NULL, rotate = FALSE, ... )
ciplot( est, se = NULL, lo = NULL, hi = NULL, SEmult = 2, labels = NULL, rotate = FALSE, ... )
est |
Vector of estimates |
se |
Vector of standard errors |
lo |
Vector of lower values for the intervals |
hi |
Vector of upper values for the intervals |
SEmult |
SE multiplier to create intervals |
labels |
Labels for the groups (vector of character strings) |
rotate |
If TRUE, have group as y-axis; default (FALSE) has group on x-axis. |
... |
Optional graphics arguments |
Calls grayplot()
with special choices of
graphics parameters, as in dotplot()
.
Provide either se
or both lo
and hi
. In the case that se
is
used, the intervals will be est
+/- SEmult * se
.
If labels
is not provided, group names are taken from the names(est)
.
If that is also missing, we use capital letters.
You can control the CI line widths with ci_lwd
and the color of
the CI segments with ci_col
. You can control the width of the
segments at the top and bottom with ci_endseg
.
None.
x <- rnorm(40, c(1,3)) g <- rep(c("A", "B"), 20) me <- tapply(x, g, mean) se <- tapply(x, g, function(a) sd(a)/sqrt(sum(!is.na(a)))) ciplot(me, se) # default is +/- 2 SE ciplot(me, se, SEmult=1) ciplot(me, se, rotate=TRUE) lo <- me - 2*se hi <- me + 2*se ciplot(me, lo=lo, hi=hi)
x <- rnorm(40, c(1,3)) g <- rep(c("A", "B"), 20) me <- tapply(x, g, mean) se <- tapply(x, g, function(a) sd(a)/sqrt(sum(!is.na(a)))) ciplot(me, se) # default is +/- 2 SE ciplot(me, se, SEmult=1) ciplot(me, se, rotate=TRUE) lo <- me - 2*se hi <- me + 2*se ciplot(me, lo=lo, hi=hi)
Convert a color to RGB and then to RGB with alpha transparency
colwalpha(color, alpha = 1)
colwalpha(color, alpha = 1)
color |
A character string for a color |
alpha |
Traparency value (between 0 and 1) |
A character string representing a color
colwalpha(c("blue", "red"), 0.5)
colwalpha(c("blue", "red"), 0.5)
For all pairs of rows in a matrix, calculate the proportion of mismatches or the RMS difference.
compare_rows(mat, method = c("prop_mismatches", "rms_difference"))
compare_rows(mat, method = c("prop_mismatches", "rms_difference"))
mat |
Numeric matrix. Should be integers in the case |
method |
Indicates whether to use proportion mismatches or the RMS difference. Missing values are omitted. |
A square matrix of dimension nrow(mat)
with
NA
s on the diagonal and the calculated statistic in the
body.
n <- 10 p <- 200 x <- matrix(sample(1:4, n*p, replace=TRUE), ncol=p) d <- compare_rows(x)
n <- 10 p <- 200 x <- matrix(sample(1:4, n*p, replace=TRUE), ncol=p) d <- compare_rows(x)
Convert a number to hexidecimal notation.
convert2hex(d)
convert2hex(d)
d |
A vector of integers (must be < 2^31). |
The input in hex, as character strings.
convert2hex(333) dec2hex(333) dec2hex(0:30)
convert2hex(333) dec2hex(333) dec2hex(0:30)
Vector of colors corresponding to Crayola crayons
crayons(color_names = NULL, ...)
crayons(color_names = NULL, ...)
color_names |
Optional vector of color names; can be partial matches. |
... |
Additional optional color names |
Vector of named RGB colors
https://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors
Like the grayplot()
function, but with one axis assumed to be categorical.
dotplot(group, y, jiggle = NULL, max_jiggle = 0.45, rotate = FALSE, ...)
dotplot(group, y, jiggle = NULL, max_jiggle = 0.45, rotate = FALSE, ...)
group |
Categorical coordinates for the plot |
y |
Coordinates of points in the plot |
jiggle |
Vector of amounts to jiggle the points horizontally,
or a character string ( |
max_jiggle |
Maximum jiggle value; passed to |
rotate |
If TRUE, have group as y-axis; default (FALSE) has group on x-axis. |
... |
Optional graphics arguments |
Calls grayplot()
with special choices of
graphics parameters for the case of categorical x.
If group
is a factor, the order of the groups is as in the
levels. Otherwise, we take sort(unique(group))
. So if you want to
control the order of the levels, make group
a factor with the levels
in the desired order, for example group <- factor(group, levels=unique(group))
.
None.
x <- rnorm(40, c(1,3)) g <- rep(c("A", "B"), 20) dotplot(g, x) dotplot(g, x, "fixed") dotplot(g, x, runif(length(g), -0.25, 0.25))
x <- rnorm(40, c(1,3)) g <- rep(c("A", "B"), 20) dotplot(g, x) dotplot(g, x, "fixed") dotplot(g, x, runif(length(g), -0.25, 0.25))
Turn a matrix of data into an SVG of how it might look in Excel
excel_fig( mat, file = NULL, cellwidth = 80, cellheight = 26, textsize = 16, fig_width = NULL, fig_height = NULL, border = "#CECECE", headcol = "#E9E9E9", headborder = "#969696", headtextcol = "#626262", textcol = "black", row_names = FALSE, col_names = TRUE, hilitcells = NULL, hilitcolor = "#F0DCDB", lwd = 1, direct2svg = FALSE, mar = rep(0.1, 4) )
excel_fig( mat, file = NULL, cellwidth = 80, cellheight = 26, textsize = 16, fig_width = NULL, fig_height = NULL, border = "#CECECE", headcol = "#E9E9E9", headborder = "#969696", headtextcol = "#626262", textcol = "black", row_names = FALSE, col_names = TRUE, hilitcells = NULL, hilitcolor = "#F0DCDB", lwd = 1, direct2svg = FALSE, mar = rep(0.1, 4) )
mat |
A matrix |
file |
Optional file name (must have extension .svg, .png, .jpg, or .pdf) |
cellwidth |
Width of each cell, in pixels |
cellheight |
Height of each cell, in pixels |
textsize |
Size for text (if |
fig_width |
Width of figure, in pixels (if NULL, taken from |
fig_height |
Height of figure, in pixels (if NULL, taken from |
border |
Color of border of cells for the body of the matrix |
headcol |
Background color of cells on the top and left border |
headborder |
Color of border of cells on the top and left border |
headtextcol |
Color of text in cells on the top and left border |
textcol |
Color of text in cells in body of the matrix |
row_names |
If TRUE, and row names are present, include them as a first column |
col_names |
If TRUE, and column names are present, include them as a first row |
hilitcells |
Optional character vector of cells to highlight, like |
hilitcolor |
Color to highlight cells, a vector of length 1 or the same length as |
lwd |
Line width for rectangles |
direct2svg |
If TRUE, rather than R graphics, just print an SVG directly with |
mar |
Plot margins, passed to |
df <- data.frame(id= c(101, 102, 103), sex= c("M", "F", "M"), weight=c(22.3, 15.8, 19.7), stringsAsFactors=FALSE) excel_fig(df, col_names=TRUE)
df <- data.frame(id= c(101, 102, 103), sex= c("M", "F", "M"), weight=c(22.3, 15.8, 19.7), stringsAsFactors=FALSE) excel_fig(df, col_names=TRUE)
exit R without saving workspace.
exit()
exit()
This just calls q("no")
None.
Convert a factor with numeric levels to a non-factor
fac2num(x)
fac2num(x)
x |
A vector containing a factor with numeric levels |
The input factor made a numeric vector
x <- factor(c(3, 4, 9, 4, 9), levels=c(3,4,9)) fac2num(x)
x <- factor(c(3, 4, 9, 4, 9), levels=c(3,4,9)) fac2num(x)
Performs a sampling version of Fisher's exact test for a two-way contingency table.
fisher(tab, n.sim = 1000)
fisher(tab, n.sim = 1000)
tab |
A matrix of counts. |
n.sim |
Number of samples of permuted tables to consider. |
This is like the function stats::fisher.test()
, but
calculates an approximate P-value rather than performing a complete
enumeration. This will be better for large, sparse tables.
A single number: the P-value testing independence of rows and columns in the table.
stats::chisq.test()
, stats::fisher.test()
, chisq()
TeaTasting <- matrix(c(3,1,1,3),nrow=2) fisher(TeaTasting,1000)
TeaTasting <- matrix(c(3,1,1,3),nrow=2) fisher(TeaTasting,1000)
Determine the precision of a number, as the number of digits past the decimal point.
get_precision(x, ...)
get_precision(x, ...)
x |
A numeric vector |
... |
Ignore this |
If the number is expressed in scientific notation, we take the number of digits
A vector of integers, with the number of digits (to the last non-zero digit) past the decimal point.
Like the plot function, but using a gray background just for the plot region.
grayplot( x, y = NULL, ..., type = "p", hlines = NULL, hlines.col = "white", hlines.lty = 1, hlines.lwd = 1, vlines = NULL, vlines.col = "white", vlines.lty = 1, vlines.lwd = 1, xat = NULL, yat = NULL, bgcolor = "gray90", pch = 21, bg = "lightblue", col = "black", v_over_h = FALSE )
grayplot( x, y = NULL, ..., type = "p", hlines = NULL, hlines.col = "white", hlines.lty = 1, hlines.lwd = 1, vlines = NULL, vlines.col = "white", vlines.lty = 1, vlines.lwd = 1, xat = NULL, yat = NULL, bgcolor = "gray90", pch = 21, bg = "lightblue", col = "black", v_over_h = FALSE )
x |
Coordinates of points in the plot |
y |
Coordinates of points in the plot (optional) |
... |
Optional graphics arguments |
type |
Plot type (points, lines, etc.) |
hlines |
Locations of horizontal grid lines; use |
hlines.col |
Colors of horizontal grid lines |
hlines.lty |
Line type of horizontal grid lines |
hlines.lwd |
Line width of horizontal grid lines |
vlines |
Locations of vertical grid lines; use |
vlines.col |
Colors of vertical grid lines |
vlines.lty |
Line type of vertical grid lines |
vlines.lwd |
Line width of vertical grid lines |
xat |
Locations for x-axis labels; |
yat |
Locations for y-axis labels; |
bgcolor |
Background color |
pch |
point type |
bg |
Background color in points |
col |
Color of outer circle in points |
v_over_h |
If |
Calls plot()
with type="n"
, then graphics::rect()
to
get the background, and then graphics::points()
. Additional
arguments you can include: mgp.x
and mgp.y
(like mgp
, for
controlling parameters of axis labels, but separate for x- and
y-axis).
None.
dotplot()
, timeplot()
, graphics::par()
, graphics::rect()
, graphics::points()
x <- rnorm(100) y <- x+rnorm(100, 0, 0.7) grayplot(x, y, col="slateblue", pch=16) at <- seq(-3, 3) grayplot(x, y, col="violetred", pch=16, hlines=at, vlines=at) grayplot(x, col="Orchid", pch=16, bgcolor="gray80", hlines=seq(-4, 4, by=0.5), hlines.lwd=c(3,1), vlines=seq(0, 100, by=5), vlines.lwd=c(3,1,1,1))
x <- rnorm(100) y <- x+rnorm(100, 0, 0.7) grayplot(x, y, col="slateblue", pch=16) at <- seq(-3, 3) grayplot(x, y, col="violetred", pch=16, hlines=at, vlines=at) grayplot(x, col="Orchid", pch=16, bgcolor="gray80", hlines=seq(-4, 4, by=0.5), hlines.lwd=c(3,1), vlines=seq(0, 100, by=5), vlines.lwd=c(3,1,1,1))
Scatterplot with a gray background and with points with missing values shown in separate panels near the margins.
grayplot_na( x, y = NULL, type = "p", bgcolor = "gray90", v_over_h = FALSE, pch = 21, bg = "lightblue", col = "black", force = c("none", "x", "y", "both"), ... )
grayplot_na( x, y = NULL, type = "p", bgcolor = "gray90", v_over_h = FALSE, pch = 21, bg = "lightblue", col = "black", force = c("none", "x", "y", "both"), ... )
x |
Coordinates of points in the plot |
y |
Coordinates of points in the plot (optional) |
type |
Plot type (points, lines, etc.) |
bgcolor |
Background color |
v_over_h |
If |
pch |
point type |
bg |
Background color in points |
col |
Color of outer circle in points |
force |
Indicates whether to force the NA box (on the x-axis, y-axis, or both) even when there are no missing values. |
... |
Optional graphics arguments |
Calls plot()
with 'type="n", then
graphics::rect()
to get the background, and then
graphics::points()
.
There are a bunch of hidden graphical arguments you can include:
na.width
controls the proportional width devoted to the NA boxes,
and na.gap
the proportion for the gap between the NA boxes and
the main plot region. mgp.x
and mgp.y
(like mgp
, for
controlling parameters of axis labels, but separate for x- and
y-axis). Also hlines
to indicate locations of of horizontal
gridlines, and hlines.col
, hlines.lwd
, and hlines.lty
to set
their color, width, and type. hlines=NA
suppresses the grid
lines. Similarly vlines
, vlines.col
, vlines.lwd
, and
vlines.lty
. xat
and yat
are for specifying the locations of
x- and y-axis labels, respectively. xat=NA
and yat=NA
indicate
no labels.
None.
n <- 100 x <- rnorm(n) y <- x+rnorm(n, 0, 0.7) x[sample(n, 10)] <- NA grayplot_na(x, y) grayplot_na(x, y, force="y") y[sample(n, 10)] <- NA grayplot_na(x, y)
n <- 100 x <- rnorm(n) y <- x+rnorm(n, 0, 0.7) x[sample(n, 10)] <- NA grayplot_na(x, y) grayplot_na(x, y, force="y") y[sample(n, 10)] <- NA grayplot_na(x, y)
View the html version of a help file while running R via ESS within emacs.
h(...)
h(...)
... |
Help topics. |
This just calls the function utils::help()
using the
argument htmlhelp=TRUE
.
No return value.
utils::help()
, utils::help.start()
h(read.cross)
h(read.cross)
Convert a number from hexidecimal to decimal notation.
hex2dec(h)
hex2dec(h)
h |
Vector of character strings with hexadecimal representation of integers
(values >= 2^31 converted to missing, |
The input converted from hexadecimal to decimal notation.
Karl W Broman, [email protected]
hex2dec("14D") hex2dec(0:30)
hex2dec("14D") hex2dec(0:30)
Utility function to plot histogram with graphics::lines()
.
histlines(x, y = NULL, breaks, use = c("counts", "density"))
histlines(x, y = NULL, breaks, use = c("counts", "density"))
x |
Either vector of breaks or the data itself. |
y |
Optional vector of density/counts, with length = |
breaks |
Breaks for histogram, if |
use |
Whether to use |
If x
and y
are both provided, x
is interpreted to
be the breaks for a histogram, and y
is a vector of counts or
density values for each interval. These are then revised so that they
may be plotted with graphics::lines()
.
If y
is NULL, x
is taken to be the data. In this
case graphics::hist()
is called with breaks=breaks
, and
either the counts
or density
are used as y
.
A data.frame with two columns: x
and y
.
graphics::hist()
, graphics::lines()
x <- rnorm(1000, mean=20, sd=5) # basic use out <- hist(x, breaks=60, plot=FALSE) plot(histlines(out$breaks, out$counts), type="l", lwd=2, xlab="x", ylab="counts", las=1) # alternative use plot(histlines(x, breaks=60, use="density"), type="l", lwd=2, xlab="x", ylab="Density", las=1) # comparing two distributions z <- rnorm(1000, mean=25, sd=5) br <- seq(min(c(x,z)), max(c(x,z)), len=50) xlines <- histlines(x, breaks=br, use="density") zlines <- histlines(z, breaks=br, use="density") ymx <- max(c(xlines$y, zlines$y))*1.05 plot(xlines, ylim=c(0, ymx), yaxs="i", xaxs="i", type="l", lwd=2, xlab="x", ylab="Density", las=1, col="blue") lines(zlines, lwd=2 , col="red")
x <- rnorm(1000, mean=20, sd=5) # basic use out <- hist(x, breaks=60, plot=FALSE) plot(histlines(out$breaks, out$counts), type="l", lwd=2, xlab="x", ylab="counts", las=1) # alternative use plot(histlines(x, breaks=60, use="density"), type="l", lwd=2, xlab="x", ylab="Density", las=1) # comparing two distributions z <- rnorm(1000, mean=25, sd=5) br <- seq(min(c(x,z)), max(c(x,z)), len=50) xlines <- histlines(x, breaks=br, use="density") zlines <- histlines(z, breaks=br, use="density") ymx <- max(c(xlines$y, zlines$y))*1.05 plot(xlines, ylim=c(0, ymx), yaxs="i", xaxs="i", type="l", lwd=2, xlab="x", ylab="Density", las=1, col="blue") lines(zlines, lwd=2 , col="red")
Spread points out horizontally so that, in dot plot of quantitative response in multiple categories, the separate points can be seen.
jiggle( group, y, method = c("random", "fixed"), hnum = 35, vnum = 40, maxvalue = 0.45 )
jiggle( group, y, method = c("random", "fixed"), hnum = 35, vnum = 40, maxvalue = 0.45 )
group |
Categorical variable defining group; can be a factor, character, or numeric vector |
y |
Vector of quantitative responses |
method |
What method to use for horizontal jiggling. |
hnum |
Number of horizontal bins for the jiggling. |
vnum |
Number of vertical bins for the jiggling. |
maxvalue |
Maximum value in the results; results will be scaled to this value.
Use |
The "random"
method is similar to
base::jitter()
but with amount of jiggling proportional
to the number of nearby points. The "fixed"
method is
similar to the beeswarm package
Numeric vector with amounts to jiggle the points horizontally
Sys.Date as a string, in a few different formats
kbdate(format = c("dateonly", "standard"), date = Sys.time())
kbdate(format = c("dateonly", "standard"), date = Sys.time())
format |
The format for the output |
date |
The date/time to convert |
A character string representation of the date/time
base::Sys.time()
, base::date()
kbdate() kbdate("standard")
kbdate() kbdate("standard")
Get the number of unique values in a vector
lenuniq(vec, na.rm = TRUE)
lenuniq(vec, na.rm = TRUE)
vec |
A vector |
na.rm |
If |
It just does length(unique(vec))
or, if
na.rm=TRUE
(the default)
length(unique(vec[!is.na(vec)]))
Number of unique values.
x <- c(1, 2, 1, 3, 1, 1, 2, 2, 3, NA, NA, 1) lenuniq(x) lenuniq(x, na.rm=FALSE)
x <- c(1, 2, 1, 3, 1, 1, 2, 2, 3, NA, NA, 1) lenuniq(x) lenuniq(x, na.rm=FALSE)
Run make within a package directory
make(pkg = ".", makefile = "Makefile", target = "", quiet = FALSE)
make(pkg = ".", makefile = "Makefile", target = "", quiet = FALSE)
pkg |
Path to directory containing the GNU Make file, or an
Rpackage description, which can be a path or a package name. (See
|
makefile |
File name of makefile. |
target |
Optional character string specifying the target. |
quiet |
If TRUE suppresses output from this function. |
Exit value from base::system()
with intern=FALSE
## Not run: make() # run make within working directory make("/path/to/mypackage") # run make within /path/to/mypackage ## End(Not run)
## Not run: make() # run make within working directory make("/path/to/mypackage") # run make within /path/to/mypackage ## End(Not run)
Boxplot-like figure for many groups, with lines connecting selected quantiles.
manyboxplot( x, probs = c(0.05, 0.1, 0.25), dotcol = "blue", linecol = c("black", "red", "green", "orange"), ... )
manyboxplot( x, probs = c(0.05, 0.1, 0.25), dotcol = "blue", linecol = c("black", "red", "green", "orange"), ... )
x |
Matrix of data, with columns indicating the groups. |
probs |
Numeric vecotr of probabilities with values in [0,1). Quantiles will be symmetric, and the median will always be included. |
dotcol |
Color for median |
linecol |
Line colors, same length as |
... |
Additional graphics parameters |
Calculates quantiles of the columns of x
and then plots dots or
lines at median plus lines at a series of quantiles, using
grayplot()
for the actual plot.
None.
mu <- c(rnorm(50, 0, 0.3), rnorm(50, 2, 0.3)) # vector of means x <- t(matrix(rnorm(1000*100, mu), ncol=1000)) manyboxplot(x, c(0.05, 0.25), ylim=range(x), dotcol=c("blue","green")[(1:100 > 50) + 1], hlines=seq(-4, 6, by=2), vlines=c(1, seq(20, 100, by=20)))
mu <- c(rnorm(50, 0, 0.3), rnorm(50, 2, 0.3)) # vector of means x <- t(matrix(rnorm(1000*100, mu), ncol=1000)) manyboxplot(x, c(0.05, 0.25), ylim=range(x), dotcol=c("blue","green")[(1:100 > 50) + 1], hlines=seq(-4, 6, by=2), vlines=c(1, seq(20, 100, by=20)))
Take the maximum of the absolute values of the input
maxabs(x, na.rm = FALSE)
maxabs(x, na.rm = FALSE)
x |
a numeric vector or array |
na.rm |
a logical indicating whether missing values should be removed. |
The maximum of the absolute value of the input
x <- c(5, -2, 8, -20, 2.3) maxabs(x)
x <- c(5, -2, 8, -20, 2.3) maxabs(x)
A matrix of scatterplots is produced; it's similar to
graphics::pairs()
, but with only the upper triangle is
made.
mypairs(x, ...)
mypairs(x, ...)
x |
A numeric matrix or data frame. |
... |
Passed to the |
This is like the function graphics::pairs()
, but
only the upper triangle is produced.
None.
v <- rbind(c(1,0.5,0.2),c(0.5,1,0.9),c(0.2,0.9,1)) x <- rmvn(500, rep(5,3), v) mypairs(x, col=sample(c("blue","red"), 500, repl=TRUE))
v <- rbind(c(1,0.5,0.2),c(0.5,1,0.9),c(0.2,0.9,1)) x <- rmvn(500, rep(5,3), v) mypairs(x, col=sample(c("blue","red"), 500, repl=TRUE))
Round a number, preserving extra 0's.
myround(x, digits = 1)
myround(x, digits = 1)
x |
Number to round. |
digits |
Number of digits past the decimal point to keep. |
Uses base::sprintf()
to round a number, keeping extra 0's.
A vector of character strings.
base::round()
, base::sprintf()
myround(51.01, 3) myround(0.199, 2)
myround(51.01, 3) myround(0.199, 2)
Quantile normalizes two vectors or a matrix.
normalize(x, y = NULL)
normalize(x, y = NULL)
x |
Numeric vector or matrix |
y |
Optional second numeric vector |
We sort the columns, take averages across rows, and then plug the averages back into the respective positions. The marginal distributions in the columns are thus forced to be the same. Missing values, which can result in differing numbers of observed values per column, are dealt with by linear interpolation.
If two vectors, x
and y
, are provided, the output is a
matrix with two columns, with the quantile normalized versions of
x
and y
.
If y
is missing, x
should be a matrix, in which case the
output is a matrix of the same dimensions with the columns quantile
normalized with respect to each other.
z <- rmvn(10000, mu=c(0,5,10), V = rbind(c(1,0.5,0.5),c(0.5,1,0.5),c(0.5,0.5,1))) z[sample(prod(dim(z)), 1500)] <- NA pairs(z) br <- seq(min(z, na.rm=TRUE), max(z, na.rm=TRUE), length=200) par(mfrow=c(3,1)) for(i in 1:3) hist(z[,i], xlab="z", main=i, breaks=br) zn <- normalize(z) br <- seq(min(zn, na.rm=TRUE), max(zn, na.rm=TRUE), length=200) for(i in 1:3) hist(zn[,i], xlab="normalized z", main=i, breaks=br) pairs(zn)
z <- rmvn(10000, mu=c(0,5,10), V = rbind(c(1,0.5,0.5),c(0.5,1,0.5),c(0.5,0.5,1))) z[sample(prod(dim(z)), 1500)] <- NA pairs(z) br <- seq(min(z, na.rm=TRUE), max(z, na.rm=TRUE), length=200) par(mfrow=c(3,1)) for(i in 1:3) hist(z[,i], xlab="z", main=i, breaks=br) zn <- normalize(z) br <- seq(min(zn, na.rm=TRUE), max(zn, na.rm=TRUE), length=200) for(i in 1:3) hist(zn[,i], xlab="normalized z", main=i, breaks=br) pairs(zn)
The numbers 1-20 spelled out in English, for use in reports.
A vector of character strings
numbers
- lower case
Numbers
- Capitalized
numbers[5] Numbers[5]
numbers[5] Numbers[5]
Calculate the sizes of all of the objects in one's workspace.
objectsizes(obj = NULL, sortbysize = TRUE)
objectsizes(obj = NULL, sortbysize = TRUE)
obj |
Vector of object names. If missing, we pull out all object names. |
sortbysize |
If TRUE, sort the objects from smallest to largest. |
Calls utils::object.size()
repeated to get the size of a
list of objects.
A data frame with the only column being the size of each object in megabytes (MB). The row names are the names of the objects.
utils::object.size()
, base::objects()
print(output <- objectsizes()) ## Not run: sum(output)
print(output <- objectsizes()) ## Not run: sum(output)
Open a file using [base::system() and "open"
(well, actually "start"
on Linux).
openfile(file)
openfile(file)
file |
File name (character string) |
I'd thought that to open a file you'd use open
in
MacOS and start
in Windows, but
system("start myfile.pdf")
doesn't work in Windows, and
rather system("open myfile.pdf")
does, so here we're
just using open
, except on Linux where at least on my
system, you can use "start"
.
None.
## Not run: openfile("myplot.pdf")
## Not run: openfile("myplot.pdf")
Calculates a p-value for a paired t-test via permutations.
paired.perm.test(d, n.perm = NULL, pval = TRUE)
paired.perm.test(d, n.perm = NULL, pval = TRUE)
d |
A numeric vector (of differences). |
n.perm |
Number of permutations to perform. If NULL, all possible permutations are considered, and an exact p-value is calculated. |
pval |
If TRUE, return just the p-value. If FALSE, return the
actual permutation results (with the observed statistic as an
attribute, |
This calls the function stats::t.test()
to calculate a
t-statistic comparing the mean of d
to 0. Permutations
are perfomed to give an exact or approximate conditional p-value.
If pval=TRUE
, the output is a single number: the P-value
testing for the symmetry about 0 of the distribution of the population
from which d
was drawn.
If pval=FALSE
, the output is a vector of the t statistics from
the permutations. An attributed "tobs"
contains the t
statistic with the observed data.
x <- c(43.3, 57.1, 35.0, 50.0, 38.2, 31.2) y <- c(51.9, 95.1, 90.0, 49.7, 101.5, 74.1) paired.perm.test(x-y)
x <- c(43.3, 57.1, 35.0, 50.0, 38.2, 31.2) y <- c(51.9, 95.1, 90.0, 49.7, 101.5, 74.1) paired.perm.test(x-y)
Calls base::paste()
with sep="."
.
paste.(...)
paste.(...)
... |
Passed to paste. |
There's not much to this function. It just is
base::paste()
with sep=""
, 'cause I'm lazy.
A character string or vector of character strings.
base::paste()
,
base::paste0()
,
paste00()
,
paste..()
,
paste0.()
,
paste.0()
x <- 3 y <- 4 paste.(x, y)
x <- 3 y <- 4 paste.(x, y)
Call base::paste()
with sep="."
or sep=""
and collapse=""
or collapse="."
.
paste00(...)
paste00(...)
... |
Passed to paste. |
There's not much to these functions.
paste00(...)
is like paste(..., sep="", collapse="")
paste..(...)
is like paste(..., sep=".", collapse=".")
paste0.(...)
is like paste(..., sep="", collapse=".")
paste.0(...)
is like paste(..., sep=".", collapse="")
A character string or vector of character strings.
base::paste()
,
base::paste0()
,
paste.()
x <- c(3, 4) y <- c(5, 6) paste00(x, y) paste..(x, y) paste0.(x, y) paste.0(x, y)
x <- c(3, 4) y <- c(5, 6) paste00(x, y) paste..(x, y) paste0.(x, y) paste.0(x, y)
Calculates a p-value for a t-test via permutations.
perm.test(x, y, n.perm = NULL, var.equal = TRUE, pval = TRUE)
perm.test(x, y, n.perm = NULL, var.equal = TRUE, pval = TRUE)
x |
A numeric vector. |
y |
A second numeric vector. |
n.perm |
Number of permutations to perform. If NULL, all possible permutations are considered, and an exact p-value is calculated. |
var.equal |
A logical variable indicating whether to treat the two population variances as being equal. |
pval |
If TRUE, return just the p-value. If FALSE, return the
actual permutation results (with the observed statistic as an
attribute, |
This calls the function stats::t.test()
to calculate a
t-statistic comparing the vectors x
and y
. Permutations
are perfomed to give an exact or approximate conditional p-value.
If pval=TRUE
, the output is a single number: the P-value
testing for a difference in the distributions of the populations from
which x
and y
were drawn.
If pval=FALSE
, the output is a vector of the t statistics from
the permutations. An attributed "tobs"
contains the t
statistic with the observed data.
stats::t.test()
, paired.perm.test()
x <- c(43.3, 57.1, 35.0, 50.0, 38.2, 61.2) y <- c(51.9, 95.1, 90.0, 49.7, 101.5, 74.1) perm.test(x,y)
x <- c(43.3, 57.1, 35.0, 50.0, 38.2, 61.2) y <- c(51.9, 95.1, 90.0, 49.7, 101.5, 74.1) perm.test(x,y)
Align two vectors of numbers by their names and then pick a single value from each, favoring the more precise one. If the two values differ by more than round-off error, treat the value as missing.
pick_more_precise(x, y, tol = 0.000001)
pick_more_precise(x, y, tol = 0.000001)
x |
A numeric vector |
y |
A second numeric vector |
tol |
Tolerance for differences between the values |
Okay, this is a bit weird. But suppose you have two columns of numbers that have been subjected to different quirky rounding patterns. We align the vectors using their names and then for each element we pick between the two choices, favoring the more-precise one. If one is missing, choose the non-missing value. If the two differ by more than the round-off error, treat it as missing.
A vector of combined values
Creates a plot of the crayon colors in brocolors()
plot_crayons( method2order = c("hsv", "cluster"), cex = 0.6, mar = rep(0.1, 4), bg = "white", fg = "black", border = FALSE )
plot_crayons( method2order = c("hsv", "cluster"), cex = 0.6, mar = rep(0.1, 4), bg = "white", fg = "black", border = FALSE )
method2order |
method to order colors ( |
cex |
character expansion for the text |
mar |
margin paramaters; vector of length 4 (see |
bg |
Background color |
fg |
Foreground color (for text and box outlines) |
border |
If TRUE, plot a border around each rectangle |
None
https://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors
plot_crayons()
plot_crayons()
Adds a line to a quantile-quantile plot for two datasets, from stats::qqplot()
.
(The available stats::qqline()
function works mainly for
stats::qqnorm()
, with one sample being theoretical quantiles.)
qqline2(x, y, probs = c(0.25, 0.75), qtype = 7, ...)
qqline2(x, y, probs = c(0.25, 0.75), qtype = 7, ...)
x |
The first sample |
y |
The second sample. |
probs |
numeric vector of length two, representing probabilities. Corresponding quantile pairs define the line drawn. |
qtype |
the |
... |
graphical parameters. |
Intercept and slope of the line.
stats::qqline()
, stats::qqplot()
x <- rchisq(500, 3) y <- rgamma(730, 3, 1/2) qqplot(x, y) qqline2(x, y)
x <- rchisq(500, 3) y <- rgamma(730, 3, 1/2) qqplot(x, y) qqline2(x, y)
Computes the QR decomposition of a matrix.
qr2(x, tol = 0.0000001)
qr2(x, tol = 0.0000001)
x |
A matrix whose QR decomposition is to be computed. |
tol |
The tolerance for detecting linear dependencies in the
columns of |
Calls the function base::qr()
and returns
less compact but more understandable output.
A list of two matrices: Q and R.
hilbert <- function(n) { i <- 1:n; 1/outer(i-1,i,"+") } h5 <- hilbert(5); qr2(h5)
hilbert <- function(n) { i <- 1:n; 1/outer(i-1,i,"+") } h5 <- hilbert(5); qr2(h5)
Calculate sample quantiles and their estimated standard errors.
quantileSE(x, p = 0.95, bw = NULL, na.rm = TRUE, names = TRUE)
quantileSE(x, p = 0.95, bw = NULL, na.rm = TRUE, names = TRUE)
x |
Numeric vector whose sample quantiles are wanted. |
p |
Numeric vector with values in the interval [0,1] |
bw |
Bandwidth to use in the density estimation. |
na.rm |
Logical; if true, and |
names |
Logical; if true, the column names of the result is set to
the values in |
The sample quantiles are calculated with the function
stats::quantile()
.
Standard errors are obtained by the asymptotic approximation described
in Cox and Hinkley (1974). Density values are estimated using a
kernel density estimate with the function stats::density()
.
A matrix of size 2 x length(p)
. The first row contains the
estimated quantiles; the second row contains the corresponding
estimated standard errors.
stats::quantile()
, stats::density()
quantileSE(rchisq(1000,4), c(0.9,0.95))
quantileSE(rchisq(1000,4), c(0.9,0.95))
Calls grDevices::gray()
then base::rev()
revgray(n = 256, ...)
revgray(n = 256, ...)
n |
Number of colors. |
... |
Passed to |
There's not much to this. It's just gray((n:0)/n))
Vector of colors, from white to black
x <- matrix(rnorm(100), ncol=10) image(x, col=revgray())
x <- matrix(rnorm(100), ncol=10) image(x, col=revgray())
Calls grDevices::rainbow()
then base::rev()
revrainbow(n = 256, ...)
revrainbow(n = 256, ...)
n |
Number of colors. |
... |
Passed to |
There's not much to this. It's just rev(rainbow(start=0, end=2/3, ...))
.
Vector of colors, from blue to red.
base::rev()
,
grDevices::rainbow()
x <- matrix(rnorm(100), ncol=10) image(x, col=revrainbow())
x <- matrix(rnorm(100), ncol=10) image(x, col=revrainbow())
Simulate from a multivariate normal distribution.
rmvn(n, mu = 0, V = matrix(1))
rmvn(n, mu = 0, V = matrix(1))
n |
Number of simulation replicates. |
mu |
Mean vector. |
V |
Variance-covariance matrix. |
Uses the Cholesky decomposition of the matrix V
, obtained by
base::chol()
.
A matrix of size n x length(mu)
. Each row corresponds to a
separate replicate.
x <- rmvn(100, c(1,2),matrix(c(1,1,1,4),ncol=2))
x <- rmvn(100, c(1,2),matrix(c(1,1,1,4),ncol=2))
Calculates a running mean, sum or median with a specified window.
runningmean( pos, value, at = NULL, window = 1000, what = c("mean", "sum", "median", "sd") )
runningmean( pos, value, at = NULL, window = 1000, what = c("mean", "sum", "median", "sd") )
pos |
Positions for the values. |
value |
Values for which the running mean/sum/median/sd is to be applied. |
at |
Positions at which running mean (or sum or median or sd) is
calculated. If NULL, |
window |
Window width. |
what |
Statistic to use. |
A vector with the same length as the input at
(or pos
,
if at
is NULL), containing the running
statistic.
Karl W Broman [email protected]
x <- 1:10000 y <- rnorm(length(x)) plot(x,y, xaxs="i", yaxs="i") lines(x, runningmean(x, y, window=100, what="mean"), col="blue", lwd=2) lines(x, runningmean(x, y, window=100, what="median"), col="red", lwd=2) lines(x, runningmean(x, y, window=100, what="sd"), col="green", lwd=2)
x <- 1:10000 y <- rnorm(length(x)) plot(x,y, xaxs="i", yaxs="i") lines(x, runningmean(x, y, window=100, what="mean"), col="blue", lwd=2) lines(x, runningmean(x, y, window=100, what="median"), col="red", lwd=2) lines(x, runningmean(x, y, window=100, what="sd"), col="green", lwd=2)
Calculates a running ratio; a ratio sum(top)/sum(bottom) in a sliding window.
runningratio(pos, numerator, denominator, at = NULL, window = 1000)
runningratio(pos, numerator, denominator, at = NULL, window = 1000)
pos |
Positions for the values. |
numerator |
Values for numerator in ratio. |
denominator |
Values for denominator in ratio. |
at |
Positions at which running ratio is
calculated. If NULL, |
window |
Window width. |
A vector with the same length as the input at
(or pos
,
if at
is NULL), containing the running ratio.
Karl W Broman [email protected]
x <- 1:1000 y <- runif(1000, 1, 5) z <- runif(1000, 1, 5) plot(x, runningratio(x, y, z, window=5), type="l", lwd=2) lines(x, runningratio(x, y, z, window=50), lwd=2, col="blue") lines(x, runningratio(x, y, z, window=100), lwd=2, col="red")
x <- 1:1000 y <- runif(1000, 1, 5) z <- runif(1000, 1, 5) plot(x, runningratio(x, y, z, window=5), type="l", lwd=2) lines(x, runningratio(x, y, z, window=50), lwd=2, col="blue") lines(x, runningratio(x, y, z, window=100), lwd=2, col="red")
Set random number generation to L'Ecuyer-CMRG, for use in parallel calculations.
setRNGparallel() unsetRNGparallel()
setRNGparallel() unsetRNGparallel()
I can never remember the command RNGkind("L'Ecuyer-CMRG")
; this is a shortcut.
unsetRNG4parallel
sets the random number generator back to the default type.
RNGkind() setRNGparallel() RNGkind() unsetRNGparallel() RNGkind()
RNGkind() setRNGparallel() RNGkind() unsetRNGparallel() RNGkind()
Perform numerical integration by Simpson's rule or the trapezoidal rule.
simp(f, a, b, tol = 0.00000001, max.step = 1000, ...)
simp(f, a, b, tol = 0.00000001, max.step = 1000, ...)
f |
The integrand; must be a vectorized function. |
a |
Lower limit of integration. |
b |
Upper limit of integration. |
tol |
Tolerance for choosing the number of grid points. |
max.step |
Log base 2 of the total number of grid points. |
... |
Other arguments passed to the integrand, |
Iterately doubles the number of grid points for the numerical
integral, stopping when the integral decreases by less than
tol
.
The integral of f
from a
to b
.
f <- function(x) x*x*(1-x)*sin(x*x) I1 <- trap(f,0,2) I2 <- simp(f,0,2)
f <- function(x) x*x*(1-x)*sin(x*x) I1 <- trap(f,0,2) I2 <- simp(f,0,2)
Spell out an integer as a word, for use in reports/papers.
spell_out(number, capitalize = FALSE, max_value = 9)
spell_out(number, capitalize = FALSE, max_value = 9)
number |
A number that is to be spelled out (can be a vector). |
capitalize |
If TRUE, capitalize the first letter. |
max_value |
Maximum value to use (generally 9); if larger than this, use numerals. |
Character string (or vector of character strings) with numbers spelled out, or as numerals if large.
spell_out(9) spell_out(9, cap=TRUE) spell_out(9, max_value=5)
spell_out(9) spell_out(9, cap=TRUE) spell_out(9, max_value=5)
Convert stringwidth
units to number of (margin) lines
strwidth2lines(s, ...)
strwidth2lines(s, ...)
s |
A character or expression vector whose length is to be calculated |
... |
additional information used by |
Maximum string width in units of margin lines
Aimee Teo Broman
p <- par(no.readonly = TRUE) string <- sapply(sample(1:20,15,replace=TRUE), function(a) paste(LETTERS[1:a], collapse="")) nlines <- strwidth2lines(string) mar <- par("mar") par(mar=c(mar[1],nlines+0.1,mar[3:4])) plot(1:length(string),1:length(string),yaxt="n", ylab="") axis(side=2, at=seq_along(string), lab=string, las=1) par(p) nlines <- strwidth2lines(string,cex=1.5) par(mar=c(mar[1:3],nlines+0.1)) plot(1:length(string),1:length(string),ylab="") mgp <- par("mgp") axis(side = 4, at=seq_along(string), labels = string ,las=1, hadj=1, mgp=c(mgp[1],nlines,mgp[3]),cex.axis=1.5) par(p)
p <- par(no.readonly = TRUE) string <- sapply(sample(1:20,15,replace=TRUE), function(a) paste(LETTERS[1:a], collapse="")) nlines <- strwidth2lines(string) mar <- par("mar") par(mar=c(mar[1],nlines+0.1,mar[3:4])) plot(1:length(string),1:length(string),yaxt="n", ylab="") axis(side=2, at=seq_along(string), lab=string, las=1) par(p) nlines <- strwidth2lines(string,cex=1.5) par(mar=c(mar[1:3],nlines+0.1)) plot(1:length(string),1:length(string),ylab="") mgp <- par("mgp") axis(side = 4, at=seq_along(string), labels = string ,las=1, hadj=1, mgp=c(mgp[1],nlines,mgp[3]),cex.axis=1.5) par(p)
Calculates the x-axis limits when adding (long) labels to a plot
strwidth2xlim(x, xstring, pos = 4, offset = 0.5, ...)
strwidth2xlim(x, xstring, pos = 4, offset = 0.5, ...)
x |
numeric vector of horizontal coordinates |
xstring |
character vector, specifying text to be written |
pos |
position specifier for text; values of |
offset |
offset of the label from the coordinate in fractions of a character width |
... |
additional text parameters from |
See text
for details on pos
and offset
.
Minimum and maximum x-axis limits for adding horizontal text
Aimee Teo Broman
x <- runif(15,-1,1)*10 xlabs <- sapply(sample(1:20,15,replace=TRUE), function(a) paste(LETTERS[1:a], collapse="")) ## Labels to the left ## xlims <- strwidth2xlim(x,xlabs,pos=2) plot(x,1:length(x),xlim=xlims) text(x,1:length(x),xlabs,pos=2) ## Labels to the right ## xlims <- strwidth2xlim(x,xlabs,pos=4,cex=0.7) plot(x,1:length(x),xlim=xlims) text(x,1:length(x),xlabs,pos=4,cex=0.7)
x <- runif(15,-1,1)*10 xlabs <- sapply(sample(1:20,15,replace=TRUE), function(a) paste(LETTERS[1:a], collapse="")) ## Labels to the left ## xlims <- strwidth2xlim(x,xlabs,pos=2) plot(x,1:length(x),xlim=xlims) text(x,1:length(x),xlabs,pos=2) ## Labels to the right ## xlims <- strwidth2xlim(x,xlabs,pos=4,cex=0.7) plot(x,1:length(x),xlim=xlims) text(x,1:length(x),xlabs,pos=4,cex=0.7)
Vectorized version of base::switch()
: just loops over
input and calls base::switch()
.
switchv(EXPR, ...)
switchv(EXPR, ...)
EXPR |
An expression evaluating to a vector of numbers of strings |
... |
List of alternatives |
Vector of returned values.
switchv(c("horse", "fish", "cat", "bug"), horse="fast", cat="cute", "what?")
switchv(c("horse", "fish", "cat", "bug"), horse="fast", cat="cute", "what?")
Karl's ggplot2 theme: black border and no ticks
theme_karl(base_size = 12, base_family = "", ...) karl_theme(base_size = 12, base_family = "", ...)
theme_karl(base_size = 12, base_family = "", ...) karl_theme(base_size = 12, base_family = "", ...)
base_size |
Base font size |
base_family |
Base font family |
... |
Passed to |
An object as returned by ggplot2::theme()
library(ggplot2) mtcars$cyl <- factor(mtcars$cyl) ggplot(mtcars, aes(y=mpg, x=disp, color=cyl)) + geom_point() + theme_karl()
library(ggplot2) mtcars$cyl <- factor(mtcars$cyl) ggplot(mtcars, aes(y=mpg, x=disp, color=cyl)) + geom_point() + theme_karl()
Set up a time-based axis for base graphics
time_axis(times, n = 8, scale = NULL, format = NULL)
time_axis(times, n = 8, scale = NULL, format = NULL)
times |
A vector of date/times that will be plotted |
n |
Number of values to use in axis |
scale |
Forced choice of scale for axis labels:
|
format |
If provided, used in place of |
A data frame with the numeric values to plot plus labels to use.
n <- 100 y <- rnorm(n) # labels as days x <- seq(as.POSIXct("2024-05-01 11:23"), as.POSIXct("2024-05-07 14:50"), length.out=n) xax <- time_axis(x) grayplot(x, y, xat=NA, vlines=xax$x) axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE) # labels as HH:MM x <- seq(as.POSIXct("2024-05-01 11:23"), as.POSIXct("2024-05-01 14:50"), length.out=n) xax <- time_axis(x) grayplot(x, y, xat=NA, vlines=xax$x) axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE) # labels as seconds x <- seq(as.POSIXct("2024-05-01 11:23:05.3"), as.POSIXct("2024-05-01 11:23:55.7"), length.out=n) xax <- time_axis(x) grayplot(x, y, xat=NA, vlines=xax$x) axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE) # custom time format xax <- time_axis(x, format="%H:%M:%S") grayplot(x, y, xat=NA, vlines=xax$x) axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE)
n <- 100 y <- rnorm(n) # labels as days x <- seq(as.POSIXct("2024-05-01 11:23"), as.POSIXct("2024-05-07 14:50"), length.out=n) xax <- time_axis(x) grayplot(x, y, xat=NA, vlines=xax$x) axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE) # labels as HH:MM x <- seq(as.POSIXct("2024-05-01 11:23"), as.POSIXct("2024-05-01 14:50"), length.out=n) xax <- time_axis(x) grayplot(x, y, xat=NA, vlines=xax$x) axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE) # labels as seconds x <- seq(as.POSIXct("2024-05-01 11:23:05.3"), as.POSIXct("2024-05-01 11:23:55.7"), length.out=n) xax <- time_axis(x) grayplot(x, y, xat=NA, vlines=xax$x) axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE) # custom time format xax <- time_axis(x, format="%H:%M:%S") grayplot(x, y, xat=NA, vlines=xax$x) axis(side=1, at=xax$x, labels=xax$label, mgp=c(2.1, 0.5, 0), tick=FALSE)
Like the grayplot()
function, but with the x-axis having date/times
timeplot(x, y, ..., n = 5, scale = NULL, format = NULL)
timeplot(x, y, ..., n = 5, scale = NULL, format = NULL)
x |
X-axis coordinates of points for the plot (must be date/time values) |
y |
Y-axis coordinates of points for the plot |
... |
Optional graphics arguments passed to |
n |
Approximate number of x-axis labels (passed to |
scale |
Passed to |
format |
Passed to |
None.
time_axis()
, grayplot()
, dotplot()
n <- 100 y <- rnorm(n) x <- seq(as.POSIXct("2024-05-01 11:23"), as.POSIXct("2024-05-01 14:50"), length.out=n) timeplot(x, y)
n <- 100 y <- rnorm(n) x <- seq(as.POSIXct("2024-05-01 11:23"), as.POSIXct("2024-05-01 14:50"), length.out=n) timeplot(x, y)
Plot an arrow within a Holmans triangle (an equilateral triangle used to depict trinomial distributions).
triarrow(x, ...)
triarrow(x, ...)
x |
A matrix with three rows and two columns, each column being a trinomial distribution. An arrow between the two points is plotted. |
... |
Passed to |
Plot of an equilateral triangle, in order to depict trinomial
distributions. A trinomial distribution (that is, a trio of
non-negative numbers that add to 1) is equated to a point in the
triangle through the distances to the three sides. This makes use of
the fact that for any point in an equilateral triangle, the sum of the
distances to the three sides is constant.
First use triplot()
to first plot the equilateral triangle.
The (x,y) coordinates of the endpoints of the arrows plotted.
triplot()
, tripoints()
,
trilines()
, tritext()
triplot() x <- cbind(c(0.9, 0.05, 0.05), c(0.8, 0.1, 0.1), c(0.1, 0.9, 0), c(0, 0.9, 0.1)) tripoints(x, lwd=2, col=c("black","blue","red","green"), pch=16) trilines(x, lwd=2, col="orange") y <- cbind(c(0.05, 0.05, 0.9), c(0.25, 0.25, 0.5)) triarrow(y, col="blue", lwd=2, len=0.1)
triplot() x <- cbind(c(0.9, 0.05, 0.05), c(0.8, 0.1, 0.1), c(0.1, 0.9, 0), c(0, 0.9, 0.1)) tripoints(x, lwd=2, col=c("black","blue","red","green"), pch=16) trilines(x, lwd=2, col="orange") y <- cbind(c(0.05, 0.05, 0.9), c(0.25, 0.25, 0.5)) triarrow(y, col="blue", lwd=2, len=0.1)
Add grid lines to a ternary plot with triplot()
trigrid( n = 1, col = "white", lty = 1, lwd = 1, outer_col = "black", outer_lwd = 2, ... )
trigrid( n = 1, col = "white", lty = 1, lwd = 1, outer_col = "black", outer_lwd = 2, ... )
n |
Number of grid lines |
col |
Color of grid lines |
lty |
Line type for grid lines |
lwd |
Line width of grid lines |
outer_col |
Color of outer triangle (If NULL, not plotted) |
outer_lwd |
Line width of outer triangle |
... |
Additional arguments passed to |
triplot(c("A","H","B"), gridlines=1, grid_lwd=2) trigrid(3, lty=2, lwd=2)
triplot(c("A","H","B"), gridlines=1, grid_lwd=2) trigrid(3, lty=2, lwd=2)
Plot lines within a Holmans triangle (an equilateral triangle used to depict trinomial distributions).
trilines(x, ...)
trilines(x, ...)
x |
A matrix with three rows, each column being a trinomial distribution. Lines between these points are plotted. |
... |
Passed to |
Plot of an equilateral triangle, in order to depict trinomial
distributions. A trinomial distribution (that is, a trio of
non-negative numbers that add to 1) is equated to a point in the
triangle through the distances to the three sides. This makes use of
the fact that for any point in an equilateral triangle, the sum of the
distances to the three sides is constant.
First use triplot()
to first plot the equilateral triangle.
The (x,y) coordinates of the endpoints of the lines plotted.
triplot()
, tripoints()
,
triarrow()
, tritext()
triplot() x <- cbind(c(0.9, 0.05, 0.05), c(0.8, 0.1, 0.1), c(0.1, 0.9, 0), c(0, 0.9, 0.1)) tripoints(x, lwd=2, col=c("black","blue","red","green"), pch=16) trilines(x, lwd=2, col="orange") y <- cbind(c(0.05, 0.05, 0.9), c(0.25, 0.25, 0.5)) triarrow(y, col="blue", lwd=2, len=0.1)
triplot() x <- cbind(c(0.9, 0.05, 0.05), c(0.8, 0.1, 0.1), c(0.1, 0.9, 0), c(0, 0.9, 0.1)) tripoints(x, lwd=2, col=c("black","blue","red","green"), pch=16) trilines(x, lwd=2, col="orange") y <- cbind(c(0.05, 0.05, 0.9), c(0.25, 0.25, 0.5)) triarrow(y, col="blue", lwd=2, len=0.1)
Plot Holmans triangle (an equilateral triangle used to depict trinomial distributions).
triplot( labels = c("(1,0,0)", "(0,1,0)", "(0,0,1)"), col = "black", lwd = 2, bgcolor = "gray90", gridlines = 0, grid_col = "white", grid_lty = 1, grid_lwd = 1, ... )
triplot( labels = c("(1,0,0)", "(0,1,0)", "(0,0,1)"), col = "black", lwd = 2, bgcolor = "gray90", gridlines = 0, grid_col = "white", grid_lty = 1, grid_lwd = 1, ... )
labels |
Labels for the three corners (lower-right, top, lower-left). |
col |
Color of edges of triangle |
lwd |
Line width for edges of triangle |
bgcolor |
Background color for triangle |
gridlines |
Number of grid lines (if 0, no grid lines will be plotted) |
grid_col |
Color of grid lines |
grid_lty |
Line type of grid lines |
grid_lwd |
Line width of grid lines |
... |
Passed to |
Plot of an equilateral triangle, in order to depict trinomial
distributions. A trinomial distribution (that is, a trio of
non-negative numbers that add to 1) is equated to a point in the
triangle through the distances to the three sides. This makes use of
the fact that for any point in an equilateral triangle, the sum of the
distances to the three sides is constant.
The triplot
function creates an empty triangle for use with the
related functions tripoints()
, trilines()
,
triarrow()
.
The (x,y) coordinates of the points plotted, if any.
tripoints()
, trilines()
,
triarrow()
, tritext()
triplot() x <- cbind(c(0.9, 0.05, 0.05), c(0.8, 0.1, 0.1), c(0.1, 0.9, 0), c(0, 0.9, 0.1)) tripoints(x, lwd=2, col=c("black","blue","red","green"), pch=16) trilines(x, lwd=2, col="orange") y <- cbind(c(0.05, 0.05, 0.9), c(0.25, 0.25, 0.5)) triarrow(y, col="blue", lwd=2, len=0.1)
triplot() x <- cbind(c(0.9, 0.05, 0.05), c(0.8, 0.1, 0.1), c(0.1, 0.9, 0), c(0, 0.9, 0.1)) tripoints(x, lwd=2, col=c("black","blue","red","green"), pch=16) trilines(x, lwd=2, col="orange") y <- cbind(c(0.05, 0.05, 0.9), c(0.25, 0.25, 0.5)) triarrow(y, col="blue", lwd=2, len=0.1)
Plot points within a Holmans triangle (an equilateral triangle used to depict trinomial distributions).
tripoints(x, ...)
tripoints(x, ...)
x |
A matrix with three rows, each column being a trinomial distribution. |
... |
Passed to |
Plot of an equilateral triangle, in order to depict trinomial
distributions. A trinomial distribution (that is, a trio of
non-negative numbers that add to 1) is equated to a point in the
triangle through the distances to the three sides. This makes use of
the fact that for any point in an equilateral triangle, the sum of the
distances to the three sides is constant.
First use triplot()
to first plot the equilateral triangle.
The (x,y) coordinates of the points plotted.
triplot()
, trilines()
,
triarrow()
, tritext()
triplot() x <- cbind(c(0.9, 0.05, 0.05), c(0.8, 0.1, 0.1), c(0.1, 0.9, 0), c(0, 0.9, 0.1)) tripoints(x, lwd=2, col=c("black","blue","red","green"), pch=16) trilines(x, lwd=2, col="orange") y <- cbind(c(0.05, 0.05, 0.9), c(0.25, 0.25, 0.5)) triarrow(y, col="blue", lwd=2, len=0.1)
triplot() x <- cbind(c(0.9, 0.05, 0.05), c(0.8, 0.1, 0.1), c(0.1, 0.9, 0), c(0, 0.9, 0.1)) tripoints(x, lwd=2, col=c("black","blue","red","green"), pch=16) trilines(x, lwd=2, col="orange") y <- cbind(c(0.05, 0.05, 0.9), c(0.25, 0.25, 0.5)) triarrow(y, col="blue", lwd=2, len=0.1)
Plot text within a Holmans triangle (an equilateral triangle used to depict trinomial distributions).
tritext(x, labels, ...)
tritext(x, labels, ...)
x |
A matrix with three rows, each column being a trinomial distribution. |
labels |
A vector of character strings, with length equal to the number of columns of |
... |
Passed to |
Plot of an equilateral triangle, in order to depict trinomial
distributions. A trinomial distribution (that is, a trio of
non-negative numbers that add to 1) is equated to a point in the
triangle through the distances to the three sides. This makes use of
the fact that for any point in an equilateral triangle, the sum of the
distances to the three sides is constant.
First use triplot()
to first plot the equilateral triangle.
Text is plotted at the (x,y) coordinates of the points.
triplot()
, trilines()
,
triarrow()
, tripoints()
triplot() x <- cbind(c(0.25, 0.5, 0.25), c(1/3, 1/3, 1/3)) tripoints(x, lwd=2, pch=21, bg="lightblue") xp <- x + c(0.02, 0, -0.02) tritext(xp, c("(1/4,1/2,1/4)", "(1/3,1/3,1/3)"), adj=c(0, 0.5))
triplot() x <- cbind(c(0.25, 0.5, 0.25), c(1/3, 1/3, 1/3)) tripoints(x, lwd=2, pch=21, bg="lightblue") xp <- x + c(0.02, 0, -0.02) tritext(xp, c("(1/4,1/2,1/4)", "(1/3,1/3,1/3)"), adj=c(0, 0.5))
Create a two-color palette from one color to another through some third color
twocolorpal(colors = c("slateblue", "white", "violetred"), n = 256, ...)
twocolorpal(colors = c("slateblue", "white", "violetred"), n = 256, ...)
colors |
Vector of three colors |
n |
Number of colors in output. |
... |
Passed to |
Vector of colors, from blue to white to red
x <- matrix(rnorm(100, 0.5), ncol=10) mxabs <- max(abs(x)) image(x, col=twocolorpal(), zlim=c(-mxabs, mxabs))
x <- matrix(rnorm(100, 0.5), ncol=10) mxabs <- max(abs(x)) image(x, col=twocolorpal(), zlim=c(-mxabs, mxabs))
Turn a vector into a single character string with the items separated by commas and an "and".
vec2string(x, conjunction = "and")
vec2string(x, conjunction = "and")
x |
A vector |
conjunction |
Word used to combine the strings |
vec2string(letters[1:2]) vec2string(letters[1:4]) vec2string(letters[1:4], "or")
vec2string(letters[1:2]) vec2string(letters[1:4]) vec2string(letters[1:4], "or")
Plot a Venn diagram (with two groups), to scale, either with circles or with squares.
venn( setA = 50, setB = 50, both = 25, method = c("circle", "square"), labels = c("A", "B"), col = c("blue", "red") )
venn( setA = 50, setB = 50, both = 25, method = c("circle", "square"), labels = c("A", "B"), col = c("blue", "red") )
setA |
Total area of set A. |
setB |
Total area of set B. |
both |
Area of intersection of sets A and B. |
method |
Indicates whether to plot circles or squares. |
labels |
Labels for the two sets. ( |
col |
Colors of the two sets. |
Plots a to-scale Venn diagram with two sets, so that the relative areas of the two sets and their intersection are exact.
None.
venn(setA=86, setB=1622, both=10) venn(setA=86, setB=1622, both=10, method="square")
venn(setA=86, setB=1622, both=10) venn(setA=86, setB=1622, both=10, method="square")
For a numeric vector, move values below and above the q and 1-q quantiles to those quantiles.
winsorize(x, q = 0.006)
winsorize(x, q = 0.006)
x |
Numeric vector |
q |
Lower quantile to use |
A vector like the input x
, but with extreme values moved in to
the q
and 1-q
quantiles.
x <- sample(c(1:10, rep(NA, 10), 21:30)) winsorize(x, 0.2)
x <- sample(c(1:10, rep(NA, 10), 21:30)) winsorize(x, 0.2)
Calculates the x-axis limits when adding (long) labels to a plot
xlimlabel(x, xlabels, pos = 4, offset = 0.5, ...)
xlimlabel(x, xlabels, pos = 4, offset = 0.5, ...)
x |
numeric vector of horizontal coordinates |
xlabels |
character vector, specifying text to be written |
pos |
position specifier for text; values of |
offset |
offset of the label from the coordinate in fractions of a character width |
... |
Additional par arguments |
See graphics::text()
for details on pos
and offset
.
Minimum and maximum x-axis limits for adding horizontal text
Aimee Teo Broman
x <- runif(15, -1, 1)*10 xlabs <- sapply(sample(1:20, 15, replace=TRUE), function(a) paste(LETTERS[1:a], collapse="")) par(mfrow=c(2,1), las=1) ## Labels to the left ## xlims <- xlimlabel(x, xlabs, pos=2) plot(x, 1:length(x), xlim=xlims, ylab="Index") text(x, 1:length(x), xlabs, pos=2) ## Labels to the right ## xlims <- xlimlabel(x, xlabs, pos=4, cex=0.7) plot(x, 1:length(x), xlim=xlims, ylab="Index") text(x, 1:length(x), xlabs, pos=4, cex=0.7)
x <- runif(15, -1, 1)*10 xlabs <- sapply(sample(1:20, 15, replace=TRUE), function(a) paste(LETTERS[1:a], collapse="")) par(mfrow=c(2,1), las=1) ## Labels to the left ## xlims <- xlimlabel(x, xlabs, pos=2) plot(x, 1:length(x), xlim=xlims, ylab="Index") text(x, 1:length(x), xlabs, pos=2) ## Labels to the right ## xlims <- xlimlabel(x, xlabs, pos=4, cex=0.7) plot(x, 1:length(x), xlim=xlims, ylab="Index") text(x, 1:length(x), xlabs, pos=4, cex=0.7)