R:使用“segment”命令在绘制点之间放置垂直线

时间:2021-11-21 23:42:43

I have a small dataframe from which I am plotting 3 columns in order to display a risk estimate and the 95% confidence intervals. Right now I have those 3 sets of vectors displayed as points, but I would like to connect them using "segment".

我有一个小数据框,我正在绘制3列,以显示风险估计和95%置信区间。现在我将这3组矢量显示为点,但我想使用“段”连接它们。

Here is a sample of the dataframe being plotted:

以下是正在绘制的数据框的示例:

                      Diagnosis  age.group X..change X..lower X..upper
1                    Dysrythmia All adults        16        0       35
2                 Heart failure All adults        -4      -20       14
3                        Asthma All adults        10       -5       28

Here is my plot code:

这是我的情节代码:

plot(dt[,4], pch="-", ylim=c(-20, 50), axes=F, ann=F, cex=1.5)
abline(h=0, col=1, lty=2)
points(dt[,3], pch=16, col="black", bg="black" )
points(dt[,5], pch="-", cex=1.5)
axis(1, at=1:10, lab=dt[,1], las=3, lwd=0, cex.axis=0.7, pos=-22)
axis(2, at=5*-20:54, las=1, cex.axis=0.7, cex.lab=0.7, col=1)
title(main="Risk estimates: All Adults", col.main="black", font.main=1)
title(ylab="Increase in risk (%)", col.lab=rgb(0,0.5,0))
box()

The points are the estimates and the dashes are the confidence intervals. I want to connect these three points for each diagnosis. I've looked at the R notation but it doesn't help me figure out how to tell R which xy "coordinates" I want to draw the segments connecting, because I have used vectors here instead of values? Can anyone help write a segment line of code? Thank you

点是估计值,破折号是置信区间。我想为每个诊断连接这三个点。我看过R符号,但它并没有帮我弄清楚如何告诉R哪个xy“坐标”我想绘制连接的段,因为我在这里使用向量而不是值?任何人都可以帮助编写段代码行吗?谢谢

1 个解决方案

#1


1  

OK, so you basically want to plot CIs. The arrows command is probably better for what you want. Here's a brief example that works with what you have for the lower CI.

好的,所以你基本上想要绘制CI。 arrows命令可能更适合你想要的东西。这是一个简短的例子,它适用于较低CI的内容。

plot(dt[,3], pch="•", ylim=c(-20, 50), axes=F, ann=F, cex=1.5, bty = 'o')
abline(h=0, col=1, lty=2)
arrows(1:3, dt[,3], 1:3, dt[,4], angle = 90, length = 0.08)

You can therefore leave out the extra points commands and any new segments commands. It's all much more concise.

因此,您可以省略额外的点命令和任何新的段命令。它更加简洁。

But, if you insist on adding segments to what you have it's just...

但是,如果你坚持要为你所拥有的东西添加细分,那就是......

segments(1:3, dt[,3], 1:3, dt[,5])
segments(1:3, dt[,3], 1:3, dt[,4])

#1


1  

OK, so you basically want to plot CIs. The arrows command is probably better for what you want. Here's a brief example that works with what you have for the lower CI.

好的,所以你基本上想要绘制CI。 arrows命令可能更适合你想要的东西。这是一个简短的例子,它适用于较低CI的内容。

plot(dt[,3], pch="•", ylim=c(-20, 50), axes=F, ann=F, cex=1.5, bty = 'o')
abline(h=0, col=1, lty=2)
arrows(1:3, dt[,3], 1:3, dt[,4], angle = 90, length = 0.08)

You can therefore leave out the extra points commands and any new segments commands. It's all much more concise.

因此,您可以省略额外的点命令和任何新的段命令。它更加简洁。

But, if you insist on adding segments to what you have it's just...

但是,如果你坚持要为你所拥有的东西添加细分,那就是......

segments(1:3, dt[,3], 1:3, dt[,5])
segments(1:3, dt[,3], 1:3, dt[,4])