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:
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.
1 Answer 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)
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))
Comments
Explore related questions
See similar questions with these tags.