箱线图和它配套的密度图

2024-04-22  本文已影响0人  小洁忘了怎么分身

文字最少的一篇哈哈哈,都是代码!
就创建示例数据→画图→拼图,完辽!

data <- data.frame(
  Group = rep(c("Group A", "Group B"), each = 100),
  Value = c(rnorm(100, mean = 10, sd = 2), rnorm(100, mean = 8, sd = 3))
)
library(ggplot2)
p1 <- ggplot(data, aes(x = Value, fill = Group)) +
  geom_density(alpha = 0.5) + 
  labs(x = "Value", y = "Density") +
  theme_minimal()
library(ggpubr)
p2 <- ggplot(data, aes(x = Group, y = Value, fill = Group)) +
  geom_boxplot(alpha = 0.5) +
  stat_compare_means(label.y = 0)+ #位置换数据要调
  labs(x = "Group", y = "Value") +
  theme_minimal()+
  theme(legend.position = "none")+
  coord_flip()

library(patchwork)
p1/p2  +
  plot_layout(heights = c(2,1),guides = "collect")&
  scale_fill_brewer(palette = "Set1")
上一篇 下一篇

猜你喜欢

热点阅读