Calculate the midpoints of a sequence of numbers. This is e.g. useful for labelling stacked barplots.

Midx(x, incl.zero = FALSE, cumulate = FALSE)

Arguments

x

the numeric vector

incl.zero

should zero be appended to x before proceeding? If TRUE the first value will be one half of the first value of x. Default is FALSE.

cumulate

should the result be calculated as cumulative sum? Default is FALSE.

Value

numeric vector with the calculated midpoins

Author

Andri Signorell <andri@signorell.net>

See also

Examples

x <- c(1, 3, 6, 7)

Midx(x)
#> [1] 2.0 4.5 6.5
Midx(x, incl.zero = TRUE)
#> [1] 0.5 2.0 4.5 6.5
Midx(x, incl.zero = TRUE, cumulate = TRUE)
#> [1]  0.5  2.5  7.0 13.5

# an alternative to
head(MoveAvg(c(0, x), order = 2, align = "l"), n = -1)
#> [1] 0.5 2.0 4.5 6.5

tab <- matrix(c(401,216,221,254,259,169), nrow=2, byrow=TRUE)
b <- barplot(tab, beside = FALSE, horiz=TRUE)

x <- t(apply(tab, 2, Midx, incl.zero=TRUE, cumulate=TRUE))
text(tab, x=x, y=b, col="red")