在 R 中使用plotCI 时更改参数。(向左或向右移动点)

发布于 2024-11-09 10:01:06 字数 167 浏览 3 评论 0原文

我知道您可以通过在代码中添加“at=1:6-0.2”或“at=1:6+0.2”来在图表上向左或向右移动箱线图,但当我使用plotCI时情况并非如此。有谁知道如何执行这个简单的参数调整?我知道这一定很简单,但是这里关于plotCI 的问题很少。它位于包 {gplots} 中。这让我发疯!感谢您的任何帮助。 -亚历克斯

I know that you can shift boxplots left or right on a graph by adding "at=1:6-0.2" or "at=1:6+0.2" to the code, but the same is not the case when I am using plotCI. Does anyone know how to perform this simple parameter adjustment? I know it has to be easy but there are very few questions about plotCI on here. It is in the package {gplots}. This is driving me crazy! Thanks for any help.
-Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不羁少年 2024-11-16 10:01:06

如果你想移动一切(点和误差线),那么你需要做的就是向plotCI的x参数添加少量:

plotCI(x=myx+0.2,y=...)

但这看起来很奇怪,所以也许你的意思是你想要将绘制在正确的位置,但将误差线稍微向右移动?这对我来说仍然很奇怪,但是可以通过获取plotCI的代码,将其放入包装函数中并向包装函数添加一个小的偏移参数(该参数传递到plotCI代码的相关部分)来相当容易地完成。

经过检查,plotCI 的代码有点长,所以我不会在这里重现整个内容。在控制台中输入plotCI,然后将结果复制并粘贴到文本文件中,然后将该函数命名为新名称,例如plotCI_offset。我相信,如果您随后在最后的 if/else 语句中更改 myarrow 函数调用的 x 坐标参数,您将获得成功。

新函数 def 看起来像这样:

plotCI_offset <- function (x, y = NULL, uiw, liw = uiw, ui, li, err = "y", ylim = NULL, 
xlim = NULL, type = "p", col = par("col"), barcol = col, 
pt.bg = par("bg"), sfrac = 0.01, gap = 1, lwd = par("lwd"), 
lty = par("lty"), labels = FALSE, add = FALSE, xlab, ylab, 
minbar, maxbar,offset=0.2, ...) 

我已经引用了下面函数的更改位:

if (!add) {
    if (invalid(labels) || labels == FALSE)
        #Add offset here to ensure plot window is right size
        plot(x+offset, y, ylim = ylim, xlim = xlim, col = col, xlab = xlab, 
            ylab = ylab, ...)
    else {
        plot(x, y, ylim = ylim, xlim = xlim, col = col, type = "n", 
            xlab = xlab, ylab = ylab, ...)
        text(x, y, label = labels, col = col, ...)
    }
}

然后就在下面更改此代码,如下所示:

if (err == "y") {
    if (gap != FALSE) 
        gap <- strheight("O") * gap
    smidge <- par("fin")[1] * sfrac
    if (!is.null(li))
        #Add offset to CIs 
        myarrows(x+offset, li, x+offset, pmax(y - gap, li), col = barcol, 
            lwd = lwd, lty = lty, angle = 90, length = smidge, 
            code = 1)
    if (!is.null(ui)) 
        myarrows(x+offset, ui, x+offset, pmin(y + gap, ui), col = barcol, 
            lwd = lwd, lty = lty, angle = 90, length = smidge, 
            code = 1)
}

这仅处理误差线垂直的情况。但水平情况下的改变是相似的。

If you want to shift everything (points and error bars) then all you need to do is add a small amount to the x parameter of plotCI:

plotCI(x=myx+0.2,y=...)

But that seems weird, so maybe you meant that you want to plot the points in the correct position, but shift the error bars slightly to the right? That still seems odd to me, but it can be done fairly easily by grabbing the code for plotCI, putting it in a wrapper function and adding a small offset parameter to your wrapper function that is passed to the relevant portion of the plotCI code.

Upon checking, that code for plotCI is a bit long, so I won't reproduce the whole thing here. Type plotCI at the console, and copy and paste the result in a text file, and call the function something new, like plotCI_offset. I believe if you then change the x coordinate parameters of the myarrow function call in the final if/else statement, you'll be golden.

The new function def would look like this:

plotCI_offset <- function (x, y = NULL, uiw, liw = uiw, ui, li, err = "y", ylim = NULL, 
xlim = NULL, type = "p", col = par("col"), barcol = col, 
pt.bg = par("bg"), sfrac = 0.01, gap = 1, lwd = par("lwd"), 
lty = par("lty"), labels = FALSE, add = FALSE, xlab, ylab, 
minbar, maxbar,offset=0.2, ...) 

And I've quoted the altered bits of the function below:

if (!add) {
    if (invalid(labels) || labels == FALSE)
        #Add offset here to ensure plot window is right size
        plot(x+offset, y, ylim = ylim, xlim = xlim, col = col, xlab = xlab, 
            ylab = ylab, ...)
    else {
        plot(x, y, ylim = ylim, xlim = xlim, col = col, type = "n", 
            xlab = xlab, ylab = ylab, ...)
        text(x, y, label = labels, col = col, ...)
    }
}

Then just below there alter this code as follows:

if (err == "y") {
    if (gap != FALSE) 
        gap <- strheight("O") * gap
    smidge <- par("fin")[1] * sfrac
    if (!is.null(li))
        #Add offset to CIs 
        myarrows(x+offset, li, x+offset, pmax(y - gap, li), col = barcol, 
            lwd = lwd, lty = lty, angle = 90, length = smidge, 
            code = 1)
    if (!is.null(ui)) 
        myarrows(x+offset, ui, x+offset, pmin(y + gap, ui), col = barcol, 
            lwd = lwd, lty = lty, angle = 90, length = smidge, 
            code = 1)
}

This only takes care of the case where the error bars are vertical. But the alterations for the horizontal case are similar.

阿楠 2024-11-16 10:01:06

试试这个:

require(plotrix)
plotCI(1:3-0.1, m1, ui1, li1, xlab="Itens", ylab="Eta2",axes=FALSE)
axis(side=1,at=1:9,label=c(x1,x2,x3),padj=0,las=1)
axis(side=2)

现在做同样的事情,但像这样:

plotCI(1:3+.1,m2, ui2, li2,axes=FALSE,col="blue",add=TRUE)

......

Try this:

require(plotrix)
plotCI(1:3-0.1, m1, ui1, li1, xlab="Itens", ylab="Eta2",axes=FALSE)
axis(side=1,at=1:9,label=c(x1,x2,x3),padj=0,las=1)
axis(side=2)

Now do the same but like this:

plotCI(1:3+.1,m2, ui2, li2,axes=FALSE,col="blue",add=TRUE)

......

权谋诡计 2024-11-16 10:01:06

我对此有一个技巧:您不移动数据点,而是移动轴上的标签

例如,您想要将数据点在 x 轴上向右移动 1,您可以隐藏 x 轴并使用新标签重新绘制它:

plotCI(..., axes=F) # just ignore the warning
axis(side=1, at=0:99, labels=1,100)

I have a hack for that: you do not move your data points, rather, you move the labels on the axes!

e.g. you want to shift your data points to the right by 1 on x-axis, you hide the x-axis and redraw it with new labels:

plotCI(..., axes=F) # just ignore the warning
axis(side=1, at=0:99, labels=1,100)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文