R绘制山脊图

1 附加包

如果尚未安装,可以直接通过install.packages('packagename')从cran下载

library(ggplot2)
library(ggridges)  # 绘制山脊图
library(dplyr)  # 数据处理
library(reshape) # 数据处理
library(viridis)  # 调色板

2 绘图

(1)对iris数据集绘制山脊图

windowsFonts('yh' = '微软雅黑')

ggplot(iris) +
  geom_density_ridges_gradient(
    aes(x = Sepal.Length, y = Species, fill = ..x..,height = ..density..), 
    scale = 1, rel_min_height = 0.01) +
  theme_ridges(font_size = 13, grid = TRUE) +
  theme(
    text = element_text(family = 'yh'),
    legend.position = 'right'
  ) +
  scale_y_discrete(name = '', expand = c(0.01, 0)) +
  scale_x_continuous(name = '', expand = c(0.01, 0)) +
  scale_fill_viridis(name = "", option = "C") 


(2)对diamonds数据集绘制山脊图

windowsFonts('yh' = '微软雅黑')

ggplot(diamonds) +
  geom_density_ridges_gradient(
    aes(x = price, y = clarity, fill = ..x..,height = ..density..), 
    scale = 3, rel_min_height = 0.01) +
  theme_ridges(font_size = 13, grid = TRUE) +
  theme(
    text = element_text(family = 'yh'),
    legend.position = 'right'
  ) +
  scale_y_discrete(name = '', expand = c(0.01, 0)) +
  scale_x_continuous(name = '', expand = c(0.01, 0)) +
  scale_fill_viridis(name = "", option = "C", alpha = .1) 


3 总结

山脊图生动展示了不同类别数据在同一因素的不同水平下的分布差异,个人感觉在纵轴方向上如果出现重叠是不太容易看清细节的,而在横轴方向更能体现分布的位置差异性。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值