使用 R 或 ggplot2 在散点图的 y 轴上绘制数据箱线图

boxplot the data on y-azis of scatter plot using R or ggplot2

boxplot the data of the y-axis of a scatter plot using matlab

我想像上面的问题一样创建图表:使用y轴作为箱线图的定量尺度,而x轴是另一个定量值。我如何在 R 中或最好在 ggplot2 中执行此操作?

group添加到假定离散值的变量:

set.seed(123)
dat <- data.frame(col1=rexp(10), col2=rep(1:2, each=5))
ggplot(dat, aes(y=col1, x=col2, group=col2)) + geom_boxplot()

如果有none,那就自己做一个:D

dat <- data.frame(col1=rexp(100), col2=rexp(100))
nxbins <- 3
dat$col3 <- cut(dat$col2, nxbins)
levels(dat$col3) <- with(dat, tapply(col2, col3, mean))
dat$col3 <- as.numeric(as.character(dat$col3))
ggplot(dat, aes(y=col1, x=col3, group=col3)) + geom_boxplot()

P.S。 当然,你准备一个可复现的例子是最好的,而不是我可耻的猜测...