0

I would like to create a box or violin plot from 2D numerical data much like the one given in Figure 1A and 1B here (Goodman, et al., Science, 2003) and given below:

2D boxplot

Is there an intuitive way to do this in ggplot2 given the x-y data?

Essentially, I think I need to first bin on x-data and then summarize the y data for plotting, but I don't have a good idea of how I might leverage ggplot functions to do this.

tonytonov
25.7k16 gold badges85 silver badges100 bronze badges
asked Aug 28, 2015 at 20:20

1 Answer 1

1

Since you haven't provided example data, I am showing a basic example using random data. You can create breaks to group your data using the function cut and then boxplot to create the chart.

Base

set.seed(12)
y <- rnorm(1000)
x <- rnorm(1000)
rng <- seq(-3, 3, 0.5)
boxplot(y ~ cut(x, breaks = rng), las = 2)

enter image description here

Using ggplot2

set.seed(12)
y <- rnorm(1000)
x <- rnorm(1000)
df <- data.frame(x = cut(x, breaks=rng), y= y)
ggplot(data = df, aes(x= x , y= y)) + geom_boxplot(aes(fill = x))

enter image description here

saladi
3,2936 gold badges39 silver badges65 bronze badges
answered Aug 28, 2015 at 21:06
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.