基本簡介
山脊圖(Ridgeline plot,以前稱為 Joyplot)是比較新的一種圖形,其主要可以用來表示幾組數(shù)據(jù)變量分布的情況糯俗。簡單的講,就是用來進行多組數(shù)據(jù)間相互比較的一種圖形睦擂。在這個例子中得湘,我們根據(jù)不同的鉆石質量來比較其價格的分布情況。山脊圖是使用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,
...
)
示例代碼
#清除環(huán)境
rm(list=ls())
#安裝需要用到的包
#install.packages("ggplot2")如果沒有安裝這里需要安裝
#install.packages("ggridges")如果沒有安裝這里需要安裝
#加載相關的包
library(ggplot2)
library(ggridges)
library(viridis)
library(hrbrthemes)
#查看數(shù)據(jù)格式
head(diamonds)#自帶的數(shù)據(jù)集
#基本繪圖
##第一種方式
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] http://www.reibang.com/p/092b373ef5bb