我知道我可以使用类似的命令将水平线添加到箱线图
abline(h=3)
Run Code Online (Sandbox Code Playgroud)
当一个面板中有多个箱形图时,是否可以为每个箱形图添加不同的水平线?
在上面的图中,我想为1添加线“ y = 1.2”,为2添加线,“ y = 1.5”,为3添加线“ y = 2.1”。
我不确定我确切地了解您想要什么,但是可能是这样:为每个箱形图添加一条线,该线与箱形图覆盖相同的x轴范围。
框的宽度pars$boxwex由默认设置为0.8的控件控制。从以下参数列表可以看出boxplot.default:
formals(boxplot.default)$pars
## list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5)
Run Code Online (Sandbox Code Playgroud)
因此,以下为每个箱线图生成一个线段:
# create sample data and box plot
set.seed(123)
datatest <- data.frame(a = rnorm(100, mean = 10, sd = 4),
b = rnorm(100, mean = 15, sd = 6),
c = rnorm(100, mean = 8, sd = 5))
boxplot(datatest)
# create data for segments
n <- ncol(datatest)
# width of each boxplot is 0.8
x0s <- 1:n - 0.4
x1s <- 1:n + 0.4
# these are the y-coordinates for the horizontal lines
# that you need to set to the desired values.
y0s <- c(11.3, 16.5, 10.7)
# add segments
segments(x0 = x0s, x1 = x1s, y0 = y0s, col = "red")
Run Code Online (Sandbox Code Playgroud)
这给出了以下图: