当前位置: 首页 > news >正文

R语言 | 峰峦图 / 山脊图

目的:为展示不同数据分布的差异。

1. ggplot2 实现

# 准备数据
dat=mtcars[, c("mpg", "cyl")]
colnames(dat)=c("value", "type")
head(dat)
#                  value type
#Mazda RX4         21.0   6
#Mazda RX4 Wag     21.0   6
#Datsun 710        22.8   4
cols=c("#F71480", "#76069A", "#FF8000")
#
p1=ggplot(dat, aes(x = value, fill = as.factor(type) ) ) +geom_density(alpha = 0.8) +scale_fill_manual(values = cols)+facet_wrap(~type, ncol=1) +  # 按气缸数分面labs(title = "Density of MPG by Cylinder Count-A",x = "Miles Per Gallon (MPG)",y = "Density",fill = "Cylinders") +theme_classic(base_size = 14)+theme(strip.background = element_blank(),  # 去掉小标题背景strip.placement = "outside");p1  # 小标题外部显示
#
p2=ggplot(dat, aes(x = value, fill = as.factor(type) ) ) +geom_density(alpha = 0.8) +scale_fill_manual(values = cols)+facet_wrap(~type, ncol=1, scales="free_y") +  # 按气缸数分面labs(title = "Density of MPG by Cylinder Count-B",x = "Miles Per Gallon (MPG)",y = "Density",fill = "Cylinders") +theme_classic(base_size = 14)+theme(strip.background = element_blank(),  # 去掉小标题背景strip.placement = "outside"); p2  # 小标题外部显示
#

在这里插入图片描述

2. 使用R包 ggridges

图放这里,方便和上图类似。

在这里插入图片描述

library(ggridges)
pB=ggplot(dat, aes(x = value, y = type, fill = factor(type, levels = c("4", "6", "8")) )) + ggridges::geom_density_ridges(alpha = 0.7, show.legend = T) +scale_fill_manual(values = cols)+#scale_y_continuous( expand = c(0,0) )+labs(title = "Density of MPG by Cylinder Count-C",x = "Miles Per Gallon (MPG)",y = "Density",fill = "Cylinders") +theme_classic(base_size = 14); pB
#
pB2=ggplot(dat, aes(x = value, y = type, fill = factor(type, levels = c("4", "6", "8")) )) + ggridges::geom_density_ridges(alpha = 0.7, show.legend = T, stat="binline", bins=25) +scale_fill_manual(values = cols)+#scale_y_continuous( expand = c(0,0) )+labs(title = "Density of MPG by Cylinder Count-D",x = "Miles Per Gallon (MPG)",y = "Density",fill = "Cylinders") +theme_classic(base_size = 14); pB2
#

3. 去掉底部的空隙

pB3=ggplot(dat, aes(x = value, y = type, fill = factor(type, levels = c("4", "6", "8")) )) + ggridges::geom_density_ridges(alpha = 0.7, show.legend = T, scale = 2) +scale_fill_manual(values = cols)+#scale_y_continuous( expand = c(0,0) )+labs(title = "Density of MPG by Cylinder Count-E\nset scale=2",x = "Miles Per Gallon (MPG)",y = "Density",fill = "Cylinders") +# 去掉底部scale_y_discrete(expand = c(0, 0)) +     # will generally have to set the `expand` optionscale_x_continuous(expand = c(0, 0)) +   # for both axes to remove unneeded paddingcoord_cartesian(clip = "on") + # to avoid clipping of the very top of the top ridgelinetheme_classic(base_size = 14); pB3

在这里插入图片描述

Ref

  • https://zhuanlan.zhihu.com/p/560080959
  • https://wilkelab.org/ggridges/
  • https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html
http://www.lryc.cn/news/498707.html

相关文章:

  • 16-03、JVM系列之:内存与垃圾回收篇(三)
  • 解决Windows与Ubuntu云服务器无法通过Socket(udp)通信问题
  • Mysql 中的锁机制
  • 12月第1周AI资讯
  • 【音频识别】数据集合集!
  • Nginx核心配置详解
  • 智能工厂的设计软件 用“力force”的性质构造智能体原型
  • Apache AGE:基于PostgreSQL的图数据库
  • RabbitMQ延迟消息的实现
  • SAP在中国:助力企业跨越成长的新篇章
  • 数据结构代码归纳
  • 数仓技术hive与oracle对比(一)
  • 筑起厂区安全--叉车安全防护装置全解析
  • 深入浅出云计算 ---笔记
  • ARINC 标准全解析:航空电子领域多系列标准的核心内容、应用与重要意义
  • SNMP 协议介绍
  • Python中的数据结构深入解析:从列表到字典的优化技巧
  • 如何利用Java爬虫获得商品类目
  • 力扣面试题 32 - 检查平衡性 C语言解法
  • 【机器学习】机器学习的基本分类-监督学习-决策树-ID3 算法
  • Implicit style-content separation using lora
  • ROS[aruco_ros+easy_handeye]手眼标定(眼在手外+UR10e+realsense-d435i)
  • 第九篇:k8s 通过helm发布应用
  • dataTable
  • json+Tomact项目报错怎么办?
  • Flume——sink连接Hive的参数配置(属性参数)
  • Netty面试内容整理-Netty 的应用场景
  • 波特图方法
  • 服务器数据恢复—硬盘掉线导致热备盘同步失败的RAID5阵列数据恢复案例
  • 在Ubuntu中运行和管理AppImage