violinjitter: create jitter in a normal-shaped envelope

View source: R/violinjitter.R

violinjitterR Documentation

create jitter in a normal-shaped envelope

Description

create jitter in a normal-shaped envelope

Usage

violinjitter(y, grouping = NULL, maxwidth = 1)

Arguments

y

numeric, the response variable (along vertical axis)

grouping

character or factor, optional, a grouping factor for y

maxwidth

positive numeric, the maximum width along the horizontal axis

Value

a numeric vector of the same length as y that represents a relative offset to be added to the actual x-coordinates

Examples

xdata <- data.frame(id = sample(letters[1:2], 500, TRUE))
xdata$resp[xdata$id == "a"] <- rnorm(n = sum(xdata$id == "a"), mean = -5, sd = 4)
xdata$resp[xdata$id == "b"] <- rnorm(n = sum(xdata$id == "b"), mean = 2, sd = 1)

plot(0, 0, type = "n", xlim = c(0.5, 2.5), ylim = c(-15, 7))
xdata$xcoord <- as.numeric(xdata$id)
points(xdata$xcoord, xdata$resp)

plot(0, 0, type = "n", xlim = c(0.5, 2.5), ylim = c(-15, 7))
xdata$xcoord2 <- xdata$xcoord + violinjitter(xdata$resp, grouping = xdata$id, maxwidth = 0.4)
points(xdata$xcoord2, xdata$resp, cex = 0.5)

gobbios/cfp documentation built on April 11, 2022, 2:22 a.m.