手动编辑绘图属性并重绘

问题描述 投票:0回答:1

我想更改基本箱线图函数显示数据的方式。

我希望将十分位数作为我的胡须,而不是箱线图当前所做的(最小-最大或四分位数范围乘以给定数字)。

因此,我尝试将箱线图保存在变量(

p
)中,并用我选择的分位数替换其“统计”属性(定义中位数、IQR 和晶须)。

但是,我无法重新绘制对象(使用 ggplot 我可能只会调用

p
,但对于箱线图,它会在我实例化它后立即绘制(即使保存到变量中),然后什么也不会发生.

x <- c(0.94063886031258,1.03136025036433,0.952395711064118,
       0.941071695551853,0.862653162449957,0.814470708156107,
       1.05880072370121,0.984234604877728,1.01529921088281,
       0.946563805766234,0.931529458894898,1.05621485359968)
set.seed(300)
x.boot <- sample(abs(1-x),replace=TRUE,size=1000)
x.q <- quantile(x.boot,c(.1,.25,.5,.75,.9))
p <- boxplot(x.boot)
p[["stats"]] <- matrix(x.q,ncol=1)
p

编辑

我看到我实际上可以这样做将绘图保存到一个可编辑的对象中,然后可以重新绘制,但这远非理想,所以如果没有其他选择,我可能会使用 ggplot 代替。

boxplot(x.boot)
# save the plot into an editable object
p <- recordPlot()
# make some edits in the right places
p[[1]][[9]][[2]][[3]] <- c(x.q[2],x.q[2],x.q[4],x.q[4])
p[[1]][[8]][[2]][[5]] <- c(x.q[1],x.q[5])
p[[1]][[8]][[2]][[3]] <- c(x.q[1],x.q[5])
p[[1]][[7]][[2]][[3]] <- c(x.q[1],x.q[5])
p[[1]][[7]][[2]][[5]] <- c(x.q[2],x.q[4])
p[[1]][[4]][[2]][[3]] <- c(x.q[2],x.q[2],x.q[4],x.q[4])
# redraw
plot.new()
p
r boxplot r-base-graphics
1个回答
0
投票

您需要的是

bxp()
。它根据给定的摘要绘制箱线图。

p1 <- p2 <- boxplot(x.boot)
x.q <- quantile(x.boot, c(.1, .25, .5, .75, .9))
p2[["stats"]] <- matrix(x.q, ncol = 1)

par(mfrow = c(1, 2))
bxp(p1, main = "Before")
bxp(p2, main = "After")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.