通过一个非常简单的例子,我希望创建一个分布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")
所需的输出应该看起来像
答案 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)