使用 hist() 和 plot() 正常工作的替代命令的箱线图错误
Boxplot error with a substitute command that works fine with hist() and plot()
我请求你帮助解决以下问题(并帮助我理解可能产生它的原因)。
我试图在箱线图的标题中粘贴一些普通文本、一个符号和一个变量的值。
我在 this website 上找到了一种简单的方法,以一些假数据和直方图为例。
x_mean <- 1.5
x_sd <- 1.2
hist(rnorm(100, x_mean, x_sd),
main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)
)
)
我很喜欢,所以我尝试了,我得到了我期待的结果。
我试过同样的情节,又一次,没问题。
我尝试用箱线图做同样的事情
x_mean <- 1.5
x_sd <- 1.2
boxplot(rnorm(100, x_mean, x_sd),
main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)
)
)
我得到了错误:
Error in paste(X[i], " ~ N(", mu, "=", 1.5, ", ", sigma^2, "=", 1.44, :
object 'X' not found
为什么?
我应该怎么做才能在箱线图中获得相同的行为?
plot
/hist
和boxplot
的区别貌似是main
是直接在plot(..., main=)
内部传递的,但是boxplot
最终通过 bxp
使用 do.call('title', list(main = ...)
绘制此文本:
如果您手动使用 title
,它会起作用:
x_mean <- 1.5
x_sd <- 1.2
boxplot(rnorm(100, x_mean, x_sd))
title(main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)
))
boxplot
和 bxp
这样做会产生错误
boxplot(rnorm(100, x_mean, x_sd))
do.call('title', list(main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)
)))
# Error in paste(X[i], " ~ N(", mu, "=", 1.5, ", ", sigma^2, "=", 1.44, :
# object 'X' not found
您可以手动完成此操作:
bxp(list(stats = matrix(1, 5), n = 1))
title(main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)
))
或者如果 bxp
使用 alist
(而不是 list
),这样 X 就不会尝试求值:
bxp(list(stats = matrix(1, 5), n = 1))
do.call('title', alist(main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)))
)
我请求你帮助解决以下问题(并帮助我理解可能产生它的原因)。
我试图在箱线图的标题中粘贴一些普通文本、一个符号和一个变量的值。
我在 this website 上找到了一种简单的方法,以一些假数据和直方图为例。
x_mean <- 1.5
x_sd <- 1.2
hist(rnorm(100, x_mean, x_sd),
main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)
)
)
我很喜欢,所以我尝试了,我得到了我期待的结果。
我试过同样的情节,又一次,没问题。
我尝试用箱线图做同样的事情
x_mean <- 1.5
x_sd <- 1.2
boxplot(rnorm(100, x_mean, x_sd),
main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)
)
)
我得到了错误:
Error in paste(X[i], " ~ N(", mu, "=", 1.5, ", ", sigma^2, "=", 1.44, :
object 'X' not found
为什么?
我应该怎么做才能在箱线图中获得相同的行为?
plot
/hist
和boxplot
的区别貌似是main
是直接在plot(..., main=)
内部传递的,但是boxplot
最终通过 bxp
使用 do.call('title', list(main = ...)
绘制此文本:
如果您手动使用 title
,它会起作用:
x_mean <- 1.5
x_sd <- 1.2
boxplot(rnorm(100, x_mean, x_sd))
title(main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)
))
boxplot
和 bxp
这样做会产生错误
boxplot(rnorm(100, x_mean, x_sd))
do.call('title', list(main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)
)))
# Error in paste(X[i], " ~ N(", mu, "=", 1.5, ", ", sigma^2, "=", 1.44, :
# object 'X' not found
您可以手动完成此操作:
bxp(list(stats = matrix(1, 5), n = 1))
title(main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)
))
或者如果 bxp
使用 alist
(而不是 list
),这样 X 就不会尝试求值:
bxp(list(stats = matrix(1, 5), n = 1))
do.call('title', alist(main = substitute(
paste(X[i], " ~ N(", mu, "=", m, ", ", sigma^2, "=", s2, ")"),
list(m = x_mean, s2 = x_sd^2)))
)