这里是我的代码(带有可重复的数据):
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()
这是一个比例的问题,即由于直方图的比例,宽度为0.5的盒子图几乎不可见。因此,为了达到所需的结果,您必须增加框图的宽度,请相应地设置位置,当然会丢弃
coord_cartesian
,例如类似:
用Reprexv2.1.1
于2025-03-18创建