I want to visualize the distribution of a variable ('F3A7_1') by plotting a histogram, and right underneath it a boxplot (below the y-axis of the histogram or something). I also want to create this plot for each of four clusters stored in the variable 'cluster_n' using facet_wrap or facet_grid.
Online, I can only find examples of doing this without faceting - like here, for example. I would greatly appreciate your help with fixing my code because it is currently absolutely not doing what I had hoped it would do... :(
Here's my code (with reproducible data):
de <- data.frame(
F3A7_1 = c(
rnorm(1000, mean = 2, sd = 1),
rnorm(1000, mean = 4, sd = 1.5),
rnorm(1000, mean = 1, sd = 0.5),
rnorm(1000, mean = 3, sd = 1)
),
cluster_n = rep(paste("Cluster", 1:4), each = 1000)
)
ggplot(de, aes(x = F3A7_1)) +
facet_wrap(~ cluster_n, scales = "free_y") +
#create histogram
geom_histogram(bins = 7,
fill = "darkseagreen2",
color = "darkgreen",
alpha = 0.7) +
#create horizontal boxplot
geom_boxplot(aes(y = -0.5, group = 1),
orientation = "y",
width = 0.5, # Thickness of the box
outlier.shape = NA,
fill = "darkseagreen2",
color = "darkgreen",
alpha = 0.7) +
#expand y-limits so that boxplot is visible
coord_cartesian(ylim = c(-1, NA)) +
theme_bw()
I want to visualize the distribution of a variable ('F3A7_1') by plotting a histogram, and right underneath it a boxplot (below the y-axis of the histogram or something). I also want to create this plot for each of four clusters stored in the variable 'cluster_n' using facet_wrap or facet_grid.
Online, I can only find examples of doing this without faceting - like here, for example. I would greatly appreciate your help with fixing my code because it is currently absolutely not doing what I had hoped it would do... :(
Here's my code (with reproducible data):
de <- data.frame(
F3A7_1 = c(
rnorm(1000, mean = 2, sd = 1),
rnorm(1000, mean = 4, sd = 1.5),
rnorm(1000, mean = 1, sd = 0.5),
rnorm(1000, mean = 3, sd = 1)
),
cluster_n = rep(paste("Cluster", 1:4), each = 1000)
)
ggplot(de, aes(x = F3A7_1)) +
facet_wrap(~ cluster_n, scales = "free_y") +
#create histogram
geom_histogram(bins = 7,
fill = "darkseagreen2",
color = "darkgreen",
alpha = 0.7) +
#create horizontal boxplot
geom_boxplot(aes(y = -0.5, group = 1),
orientation = "y",
width = 0.5, # Thickness of the box
outlier.shape = NA,
fill = "darkseagreen2",
color = "darkgreen",
alpha = 0.7) +
#expand y-limits so that boxplot is visible
coord_cartesian(ylim = c(-1, NA)) +
theme_bw()
Share
Improve this question
asked Mar 18 at 18:36
kornpatkornpat
374 bronze badges
1 Answer
Reset to default 1This is a matter of the scale, i.e. due to the scale of the histogram a box plot with a width of .5 is hardly visible. Hence, to achieve your desired result you have to increase the width for the box plots, set the position accordingly and of course drop the coord_cartesian
, e.g. something like:
library(ggplot2)
set.seed(123)
ggplot(de, aes(x = F3A7_1)) +
facet_wrap(~cluster_n) +
# create histogram
geom_histogram(
bins = 7,
fill = "darkseagreen2",
color = "darkgreen",
alpha = 0.7
) +
# create horizontal boxplot
geom_boxplot(aes(y = -100),
orientation = "y",
width = 100,
outlier.shape = NA,
fill = "darkseagreen2",
color = "darkgreen",
alpha = 0.7
) +
theme_bw()
Created on 2025-03-18 with reprex v2.1.1