将点集成到相对于第二轴的基础R图中

时间:2016-06-30 12:22:24

标签: r boxplot

通过一个非常简单的例子,我希望创建一个分布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")

所需的输出应该看起来像

enter image description here

1 个答案:

答案 0 :(得分:1)

你可以这样做:

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)

enter image description here