-2

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?

Community
  • 1
  • 1
yosukesabai
  • 6,184
  • 4
  • 30
  • 42
  • A reproducible example could help: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – bVa Aug 09 '16 at 15:55
  • What have you tried? `with(mtcars, qplot(x = cyl, y = hp, group = cyl, geom = 'boxplot'))` seems to work just fine – bouncyball Aug 09 '16 at 16:00

1 Answers1

0

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...

renato vitolo
  • 1,744
  • 11
  • 16