基本简介
山脊图(Ridgeline plot,以前称为 Joyplot)是比较新的一种图形,其主要可以用来表示几组数据变量分布的情况。简单的讲,就是用来进行多组数据间相互比较的一种图形。在这个例子中,我们根据不同的钻石质量来比较其价格的分布情况。山脊图是使用ggridges包绘制的,此包是ggplot2包的扩展,因此符合ggplot2的语法。在我们的这个例子中, x轴指定为钻石的价格,y轴指定为钻石的品质。
基本用法
geom_density_ridges {ggridges} R Documentation
Create ridgeline plot
Description
geom_density_ridges arranges multiple density plots in a staggered fashion, as in the cover of the famous Joy Division album Unknown Pleasures.
Usage
geom_density_ridges(
mapping = NULL,
data = NULL,
stat = "density_ridges",
position = "points_sina",
panel_scaling = TRUE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
geom_density_ridges2(
mapping = NULL,
data = NULL,
stat = "density_ridges",
position = "points_sina",
panel_scaling = TRUE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
示例代码
#清除环境
rm(list=ls())
#安装需要用到的包
#install.packages("ggplot2")如果没有安装这里需要安装
#install.packages("ggridges")如果没有安装这里需要安装
#加载相关的包
library(ggplot2)
library(ggridges)
library(viridis)
library(hrbrthemes)
#查看数据格式
head(diamonds)#自带的数据集
#基本绘图
##第一种方式
ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
geom_density_ridges() +
theme_ridges() +
theme(legend.position = "none")
##第二种方式
ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
geom_density_ridges(stat="binline", bins=20) +
theme_ridges() +
theme(legend.position = "none")
对基本的山脊图进行调整修改。
##调整修改
ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +
geom_density_ridges(scale = 0.5) +
theme_ridges() +
theme_bw()+
xlab("Price")+ylab("Quality")+
theme(legend.text=element_text(size=12))+
theme(title=element_text(size=14))+
theme(axis.text.x = element_text(size = 12, color = "black"))+
theme(axis.text.y = element_text(size = 12, color = "black"))+
theme(legend.position = "none")+
geom_vline(aes(xintercept=1000), colour="#990000", linetype="dashed")
参考文献
[1] https://r-graph-gallery.com/294-basic-ridgeline-plot.html
[2] https://www.jianshu.com/p/092b373ef5bb
网友评论