作者:嚴濤 浙江大學作物遺傳育種在讀研究生(生物信息學方向)偽碼農,R語言愛好者,愛開源。
簡介ggridges包主要用來繪製山巒圖。尤其是針對時間或者空間分布可視化具有十分好的效果。ggridges主要提供兩個幾何圖像函數:
具體用法可以參考官方文檔:
https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html
geom_ridgeline()library(ggridges)
library(tidyverse)
# grid.arrange來自於gridExtra包,可以同時拼圖多個ggplot2對象
library(gridExtra)
my_data <- data.frame(x=1:5, y=rep(1,5), height=c(0,1,-1,3,2))
plot_base <- ggplot(my_data, aes(x, y, height=height))
# 默認負值不顯示,除非指定min_height參數
grid.arrange(plot_base+geom_ridgeline(),
plot_base+geom_ridgeline(min_height=-2), ncol=2)
geom_density_ridges()函數首先會根據數據計算密度然後繪圖,此時美學映射height沒有必要寫入函數中。下面使用lincoln_weather數據集。
# creates a vector of n equally spaced colors along the
# Matplolib 'viridis' color map
# also designed to be perceived by readers with the most common form of color blindness
# scale_fill_viridis函數來源於此包,
# 其參數 option用於設置顏色 "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"),
and "viridis" (or "D", the default option).
# ?viridis可以查看其具體含義
library(viridis)
head(lincoln_weather[ ,1:4])
## # A tibble: 6 x 4
## CST `Max Temperature [F]` `Mean Temperature [F]` `Min Temperature ~
## <chr> <int> <int> <int>
## 1 2016-1-1 37 24 11
## 2 2016-1-2 41 23 5
## 3 2016-1-3 37 23 8
## 4 2016-1-4 30 17 4
## 5 2016-1-5 38 29 19
## 6 2016-1-6 34 33 32
# x後的值用 ` (反引號)括起,是因為列名字中存在空格和特殊字符,需要特殊對待
# fill = ..x.., double dots是ggplot2的一種特殊識別符,用來區分定義的和計算的美學參數
# 這裡指用橫軸的數據著色
ggplot(lincoln_weather, aes(x=`Mean Temperature [F]`, y=`Month`, fill=..x..))+
geom_density_ridges_gradient(scale=3, rel_min_height=0.01, gradient_lwd = 1.)+
scale_x_continuous(expand = c(0.01, 0))+ # 擴展下橫軸和縱軸
scale_y_discrete(expand = c(0.01,0))+
scale_fill_viridis(name="Temp. [F]", option = "C")+
labs(title="Temperature in Lincoln NE",
subtitle="Mean temperature (Fahrenheit) by month for 2016\nData:Orogin CSV from the Weather Underground ")+
theme_ridges(font_size = 13, grid = FALSE)+
theme(axis.title.y = element_blank())
為了使得ggridges繪製的圖形可視化效果最好,同時為了減少用戶對顏色設置的困難,作者提供了cyclinal scales用於顏色輪轉映射。
ggplot(diamonds, aes(x=price, y=cut, fill=cut))+
geom_density_ridges(scale=4)+
scale_fill_cyclical(values = c("blue", "green"))+
theme_ridges(grid = FALSE)
默認的,cyclinal scales為了防止誤解是不繪製圖例的,但是可以通過選項guide="legend"添加圖例。
ggplot(diamonds, aes(x=price, y=cut, fill=cut))+
geom_density_ridges(scale=4)+
scale_fill_cyclical(values = c("blue", "green"), guide="legend")+
theme_ridges(grid = FALSE)
跟ggplot2一樣,圖例是可以修改的,其他參數比如大小、透明度、形狀等都是可以通過cyclinal scales修改。
ggplot(diamonds, aes(x=price, y=cut, fill=cut))+
geom_density_ridges(scale=4)+
scale_fill_cyclical(values = c("blue", "green"), guide="legend",
labels=c("Fair"="blue", "Good"="green"),
name="Fill colors")+
theme_ridges(grid = FALSE)
不做解釋了,如果想重現就把代碼拆解開,按需修改。一句句話單獨拆開運行,理解其操作內容。
library(dplyr)
library(forcats)
Catalan_elections %>%
mutate(YearFct = fct_rev(as.factor(Year))) %>%
ggplot(aes(y = YearFct)) +
geom_density_ridges(
aes(x = Percent, fill = paste(YearFct, Option)),
alpha = .8, color = "white", from = 0, to = 100
) +
labs(
x = "Vote (%)",
y = "Election Year",
title = "Indy vs Unionist vote in Catalan elections",
subtitle = "Analysis unit: municipalities (n = 949)",
caption = "Marc Belzunces (@marcbeldata) | Source: Idescat"
) +
scale_y_discrete(expand = c(0.01, 0)) +
scale_x_continuous(expand = c(0.01, 0)) +
scale_fill_cyclical(
breaks = c("1980 Indy", "1980 Unionist"),
labels = c(`1980 Indy` = "Indy", `1980 Unionist` = "Unionist"),
values = c("#ff0000", "#0000ff", "#ff8080", "#8080ff"),
name = "Option", guide = "legend"
) +
theme_ridges(grid = FALSE)
library(DAAG) # for ais dataset
ais$sport <- factor(
ais$sport,
levels = c("B_Ball", "Field", "Gym", "Netball", "Row", "Swim", "T_400m", "T_Sprnt", "Tennis", "W_Polo"),
labels = c("Basketball", "Field", "Gym", "Netball", "Row", "Swim", "Track 400m", "Track Sprint", "Tennis", "Water Polo")
)
ggplot(ais, aes(x=ht, y=sport, color=sex, point_color=sex, fill=sex)) +
geom_density_ridges(
jittered_points=TRUE, scale = .95, rel_min_height = .01,
point_shape = "|", point_size = 3, size = 0.25,
position = position_points_jitter(height = 0)
) +
scale_y_discrete(expand = c(.01, 0)) +
scale_x_continuous(expand = c(0, 0), name = "height [cm]") +
scale_fill_manual(values = c("#D55E0050", "#0072B250"), labels = c("female", "male")) +
scale_color_manual(values = c("#D55E00", "#0072B2"), guide = "none") +
scale_discrete_manual("point_color", values = c("#D55E00", "#0072B2"), guide = "none") +
guides(fill = guide_legend(
override.aes = list(
fill = c("#D55E00A0", "#0072B2A0"),
color = NA, point_color = NA))
) +
ggtitle("Height in Australian athletes") +
theme_ridges(center = TRUE)
還有很多用法有興趣的可以查看官方文檔https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html和https://cran.r-project.org/web/packages/ggridges/vignettes/gallery.html)繼續學習。
嚴濤老師的繪圖教程還有:
往期精品(點擊圖片直達文字對應教程)後臺回復「生信寶典福利第一波」或點擊閱讀原文獲取教程合集