原文教程:R語(yǔ)言繪圖 | 復(fù)雜的CompelxUpset繪制教程
關(guān)于《R語(yǔ)言繪圖專欄》
關(guān)于《R語(yǔ)言繪圖專欄》,此專欄基于
R語(yǔ)言
繪制圖形贝攒。每個(gè)圖形我們會(huì)提供對(duì)應(yīng)的R代碼
、數(shù)據(jù)
和文本
文檔卧蜓。此系列將會(huì)是一個(gè)長(zhǎng)期更新的系列。
注意:若是你加入我們社群规个,則直接在社群中獲得憨栽。
本期教程
獲得本教程
Data and Code
措译,請(qǐng)?jiān)诤笈_(tái)回復(fù):20240815
。
2022年教程總匯
2023年教程總匯
Code
- 加載對(duì)應(yīng)的R包
#install.packages('ComplexUpset')
#install.packages("ggplot2movies")
library(ComplexUpset)
library(ggplot2)
library(ggplot2movies)
- 創(chuàng)建數(shù)據(jù)
movies = as.data.frame(ggplot2movies::movies)
head(movies, 3)
> head(movies, 3)
title year length budget rating votes r1 r2 r3 r4 r5 r6
1 $ 1971 121 NA 6.4 348 4.5 4.5 4.5 4.5 14.5 24.5
2 $1000 a Touchdown 1939 71 NA 6.0 20 0.0 14.5 4.5 24.5 14.5 14.5
3 $21 a Day Once a Month 1941 7 NA 8.2 5 0.0 0.0 0.0 0.0 0.0 24.5
r7 r8 r9 r10 mpaa Action Animation Comedy Drama Documentary Romance Short
1 24.5 14.5 4.5 4.5 0 0 1 1 0 0 0
2 14.5 4.5 4.5 14.5 0 0 1 0 0 0 0
3 0.0 44.5 24.5 24.5 0 1 0 0 0 0 1
- 數(shù)據(jù)處理
##'@提取對(duì)應(yīng)的數(shù)據(jù)
genres = colnames(movies)[18:24]
genres
movies[genres] = movies[genres] == 1
t(head(movies[genres], 3))
movies[movies$mpaa == '', 'mpaa'] = NA
movies = na.omit(movies)
head(movies)
set_size = function(w, h, factor=1.5) {
s = 1 * factor
options(
repr.plot.width=w * s,
repr.plot.height=h * s,
repr.plot.res=100 / factor,
jupyter.plot_mimetypes='image/png',
jupyter.plot_scale=1
)
}
- 繪制基礎(chǔ)圖形
set_size(8, 3)
upset(movies, genres, name='genre', width_ratio=0.1)
- 繪制維恩圖
abc_data = create_upset_abc_example()
abc_venn = (
ggplot(arrange_venn(abc_data))
+ coord_fixed()
+ theme_void()
+ scale_color_venn_mix(abc_data)
)
(
abc_venn
+ geom_venn_region(data=abc_data, alpha=0.05)
+ geom_point(aes(x=x, y=y, color=region), size=1)
+ geom_venn_circle(abc_data)
+ geom_venn_label_set(abc_data, aes(label=region))
+ geom_venn_label_region(
abc_data, aes(label=size),
outwards_adjust=1.75,
position=position_nudge(y=0.2)
)
+ scale_fill_venn_mix(abc_data, guide='none')
)
- 繪制復(fù)雜維恩圖
set_size(8, 8)
set.seed(0) # keep the same jitter for identical plots
upset(
movies,
genres,
annotations = list(
# 1st method - passing list:
'Length'=list(
aes=aes(x=intersection, y=length),
# provide a list if you wish to add several geoms
geom=geom_boxplot(na.rm=TRUE)
),
# 2nd method - using ggplot
'Rating'=(
# note that aes(x=intersection) is supplied by default and can be skipped
ggplot(mapping=aes(y=rating))
# checkout ggbeeswarm::geom_quasirandom for better results!
+ geom_jitter(aes(color=log10(votes)), na.rm=TRUE)
+ geom_violin(alpha=0.5, na.rm=TRUE)
),
# 3rd method - using `upset_annotate` shorthand
'Budget'=upset_annotate('budget', geom_boxplot(na.rm=TRUE))
),
min_size=10,
width_ratio=0.1
)
- 比例Upset
##'@繪制比例Upset圖
set_size(8, 5)
upset(
movies,
genres,
annotations = list(
'MPAA Rating'=(
ggplot(mapping=aes(fill=mpaa))
+ geom_bar(stat='count', position='fill')
+ scale_y_continuous(labels=scales::percent_format())
+ scale_fill_manual(values=c(
'R'='#E41A1C', 'PG'='#377EB8',
'PG-13'='#4DAF4A', 'NC-17'='#FF7F00'
))
+ ylab('MPAA Rating')
)
),
width_ratio=0.1
)
- 柱狀圖+Upset
set_size(8, 3)
upset(
movies,
genres,
base_annotations=list(
'Intersection size'=intersection_size(
counts=FALSE,
mapping=aes(fill=mpaa)
)
),
width_ratio=0.1
)
獲得本教程
Data and Code
,請(qǐng)?jiān)诤笈_(tái)回復(fù):20240815
钱豁。
若我們的教程對(duì)你有所幫助溢豆,請(qǐng)
點(diǎn)贊+收藏+轉(zhuǎn)發(fā)
卷仑,這是對(duì)我們最大的支持麸折。
往期部分文章
1. 最全WGCNA教程(替換數(shù)據(jù)即可出全部結(jié)果與圖形)
2. 精美圖形繪制教程
3. 轉(zhuǎn)錄組分析教程
4. 轉(zhuǎn)錄組下游分析
小杜的生信筆記 工秩,主要發(fā)表或收錄生物信息學(xué)教程助币,以及基于R分析和可視化(包括數(shù)據(jù)分析俭缓,圖形繪制等)惜姐;分享感興趣的文獻(xiàn)和學(xué)習(xí)資料!!