在箱线图中添加多条水平线 [英] Add multiple horizontal lines in a boxplot

查看:128
本文介绍了在箱线图中添加多条水平线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用类似的命令将水平线添加到箱线图中

I know that I can add a horizontal line to a boxplot using a command like

abline(h=3)

当一个面板中有多个箱形图时,是否可以为每个箱形图添加不同的水平线?

When there are multiple boxplots in a single panel, can I add different horizontal lines for each single boxplot?

在上面的图中,我想为1添加线y = 1.2,对于2添加线y = 1.5,为3添加线y = 2.1.

In the above plot, I would like to add lines 'y=1.2' for 1, 'y=1.5' for 2, and 'y=2.1' for 3.

推荐答案

我不确定我确切地了解您想要什么,但这可能是这样的:为每个覆盖相同x轴范围的箱形图添加一行作为箱线图.

I am not sure that I understand exactly, what you want, but it might be this: add a line for each boxplot that covers the same x-axis range as the boxplot.

框的宽度由pars$boxwex控制,默认情况下设置为0.8.这可以从boxplot.default的参数列表中看到:

The width of the boxes is controlled by pars$boxwex which is set to 0.8 by default. This can be seen from the argument list of boxplot.default:

formals(boxplot.default)$pars
## list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5)

因此,以下代码为每个箱形图生成了一个线段:

So, the following produces a line segment for each boxplot:

# 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")

这给出了以下情节:

这篇关于在箱线图中添加多条水平线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆