我知道我可以使用如下命令将一条水平线添加到箱形图中
abline(h=3)
当一个面板中有多个箱形图时,我可以为每个箱形图添加不同的水平线吗?
在上面的图中,我想为1添加'y=1.2‘行,为2添加'y=1.5’行,为3添加'y=2.1‘行。
发布于 2015-12-19 21:34:11
我不确定我是否确切地理解了您想要的内容,但可能是这样的:为覆盖与boxplot相同的x轴范围的每个boxplot添加一行。
长方体的宽度由pars$boxwex
控制,默认情况下设置为0.8。这可以从boxplot.default
的参数列表中看到
formals(boxplot.default)$pars
## list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5)
因此,下面的代码将为每个箱线图生成一条线段:
# 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")
这给出了以下图:
https://stackoverflow.com/questions/34366196
复制相似问题