美文网首页
箱线图和它配套的密度图

箱线图和它配套的密度图

作者: 小洁忘了怎么分身 | 来源:发表于2024-04-22 16:11 被阅读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")

相关文章

网友评论

      本文标题:箱线图和它配套的密度图

      本文链接:https://www.haomeiwen.com/subject/mnxixjtx.html