yosukesabai - 1 year ago 73

R Question

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

I'd like to create plot like in the question above: use y-axis as quantitative scale for box plot, while x axis is another quantitative value. How can I do this in R, or preferably in ggplot2?

Answer Source

Add `group`

to the variable assuming discrete values:

```
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()
```

If there is none, then make one yourself :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. Of course, a reproducible example prepared by you would be the best, instead of my shameful guesswork...