将点积分到相对于第二轴的基本 R 图中

integrate points into base R plot in respect to second axis

举一个非常简单的例子,我想创建一个分布 X 的箱线图,然后添加 Y 相对于第二个垂直轴的点。有人可以提供一些建议或帮助吗?

set.seed(123)
A <- rnorm(100, 4, 1)
B <- rnorm(100, 25, 5)

# create our simple boxplot
boxplot(A, ylim=c(0,10))

# then our second axis
axis(4, at = seq(0, 10, by = 1), labels= seq(0, 100, by = 10),las=2)

# want to create vertical poins for each B value
# possibly with some jitter
par(new = TRUE)
points(B, bty = "n", xlab = "", ylab = "", ylim=c(0,100), col="green")

所需的输出应该类似于

你可以这样做:

par(mar=c(4,4,1,4))
boxplot(A)
par(new = TRUE)
plot(rep(1,length(B)), B, col="green", axes = FALSE, bty = "n", xlab = "", ylab = "")
axis(4, at =seq(0, 100, by = 10))

编辑:

由于您附上了预期结果的图片,我找到了另一个使用 beeswarm

的解决方案
library(beeswarm)
boxplot(list(A, B), ylim=range(A))
par(new = TRUE)
beeswarm(list(A, B), pwcol=c(rep(0,length(A)),rep(2,length(B))), axes=FALSE, ylim=c(0, 100))
axis(4, at =seq(0, 100, by = 10), las= 2)