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` option
  scale_x_continuous(expand = c(0, 0)) +   # for both axes to remove unneeded padding
  coord_cartesian(clip = "on") + # to avoid clipping of the very top of the top ridgeline
  theme_classic(base_size = 14); pB3

Ref

相关推荐
ZVAyIVqt0UFji2 小时前
go-zero负载均衡实现原理
运维·开发语言·后端·golang·负载均衡
loop lee2 小时前
Nginx - 负载均衡及其配置(Balance)
java·开发语言·github
SomeB1oody3 小时前
【Rust自学】4.1. 所有权:栈内存 vs. 堆内存
开发语言·后端·rust
toto4123 小时前
线程安全与线程不安全
java·开发语言·安全
水木流年追梦4 小时前
【python因果库实战10】为何需要因果分析
开发语言·python
w(゚Д゚)w吓洗宝宝了5 小时前
C vs C++: 一场编程语言的演变与对比
c语言·开发语言·c++
AI人H哥会Java5 小时前
【Spring】Spring的模块架构与生态圈—Spring MVC与Spring WebFlux
java·开发语言·后端·spring·架构
开心工作室_kaic5 小时前
springboot461学生成绩分析和弱项辅助系统设计(论文+源码)_kaic
开发语言·数据库·vue.js·php·apache
觉醒的程序猿6 小时前
vue2设置拖拽选中时间区域
开发语言·前端·javascript
明月看潮生6 小时前
青少年编程与数学 02-004 Go语言Web编程 12课题、本地数据存储
开发语言·青少年编程·本地存储·编程与数学·goweb