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

相关推荐
C++ 老炮儿的技术栈3 小时前
UDP 与 TCP 的区别是什么?
开发语言·c++·windows·算法·visual studio
wgslucky3 小时前
Dubbo报错:module java.base does not “opens java.lang“ to unnamed module
java·开发语言·dubbo
whyeekkk4 小时前
python打卡第48天
开发语言·python
DougLiang5 小时前
关于easyexcel动态下拉选问题处理
java·开发语言
全职计算机毕业设计5 小时前
基于Java Web的校园失物招领平台设计与实现
java·开发语言·前端
5:006 小时前
云备份项目
linux·开发语言·c++
笨笨马甲6 小时前
Qt Quick模块功能及架构
开发语言·qt
夜晚回家7 小时前
「Java基本语法」代码格式与注释规范
java·开发语言
YYDS3147 小时前
C++动态规划-01背包
开发语言·c++·动态规划
前端页面仔7 小时前
易语言是什么?易语言能做什么?
开发语言·安全