ComplexUpset---強大的Veen繪制包

ComplexUpset是非常強的Veen圖繪制軟件包,平時Veen的繪制最多能達(dá)到6個相交,而此包對于就是針對6個以及以上的交互所準(zhǔn)備的。特此推薦,以下模板可直接套用。

一、安裝包

install.packages("ggplot2")
install.packages("ComplexUpset")
install.packages("ggplot2movies")

二、加載包

library(ggplot2)
library(ComplexUpset)
library(ggplot2movies)

三、加載數(shù)據(jù)

movies = as.data.frame(ggplot2movies::movies)
head(movies, 3)
genres = colnames(movies)[18:24]
genres
#將genres轉(zhuǎn)換為布爾型
movies[genres] = movies[genres] == 1
t(head(movies[genres], 3))#倒置前三行進(jìn)行查看
##創(chuàng)建子集以便快速運行
movies[movies$mpaa == '', 'mpaa'] = NA
movies = na.omit(movies)#刪除缺失值
movies

四、自定義函數(shù)定義圖片大小

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
  )
}
set_size(8, 3)

五、基本用法

upset(movies, genres, name='genre', width_ratio=0.1)
image

雙圖展示:

(
  upset(movies, genres, name='genre', width_ratio=0.1, min_size=10, wrap=TRUE, set_sizes=FALSE)
  + ggtitle('Without empty groups (Short dropped)')
  +    # adding plots is possible thanks to patchwork
    upset(movies, genres, name='genre', width_ratio=0.1, min_size=10, keep_empty_groups=TRUE, wrap=TRUE, set_sizes=FALSE)
  + ggtitle('With empty groups')
)
image

展示所有交互:

set_size(8, 3)
upset(
  movies, genres,
  width_ratio=0.1,
  min_size=10,
  mode='inclusive_union',
  base_annotations=list('Size'=(intersection_size(counts=FALSE, mode='inclusive_union'))),
  intersections="all",
  max_degree=3
)
image

添加組件

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
)
image

改變數(shù)字顏色以及調(diào)整文字方向

set_size(8, 3)
upset(movies,genres,
  base_annotations=list(
    'Intersection size'=intersection_size(
      text=list(
        vjust=-0.1,
        hjust=-0.1,
        angle=45),
      text_colors=c(
        on_background='brown', on_bar='yellow'))
    + annotate(
      geom='text', x=Inf, y=Inf,
      label=paste('Total:', nrow(movies)),
      vjust=1, hjust=1
    )
    + ylab('Intersection size')),
  min_size=10,
  width_ratio=0.1
)
image

給柱子添加顏色

set_size(8, 3)
upset(movies,genres,
  base_annotations=list(
    'Intersection size'=intersection_size(
      text=list(
        vjust=-0.1,
        hjust=-0.1,
        angle=45),      
      counts=T,
      mapping=aes(fill=mpaa),
      text_colors=c(
        on_background='brown', on_bar='yellow'))
    + annotate(
      geom='text', x=Inf, y=Inf,
      label=paste('Total:', nrow(movies)),
      vjust=1, hjust=1
    )
    + ylab('Intersection size')),
  min_size=10,
  width_ratio=0.1
)
image

修改背景等

set_size(8, 3)
upset(movies,genres,
  base_annotations=list(
    'Intersection size'=intersection_size(
      text=list(
        vjust=-0.1,
        hjust=-0.1,
        angle=45),      
      counts=T,
      mapping=aes(fill=mpaa),
      text_colors=c(
        on_background='brown', on_bar='yellow'))
    + annotate(
      geom='text', x=Inf, y=Inf,
      label=paste('Total:', nrow(movies)),
      vjust=1, hjust=1
    )
    +theme(plot.background=element_rect(fill='#E5D3B3'))
    + ylab('Intersection size')),
  set_sizes=(
      upset_set_size()
      + theme(axis.text.x=element_text(angle=90))),
  min_size=10,
  width_ratio=0.1
)
image

下部添加顏色

set_size(8, 3)
upset(movies,genres,
  base_annotations=list(
    'Intersection size'=intersection_size(
      text=list(
        vjust=-0.1,
        hjust=-0.1,
        angle=45),      
      counts=T,
      mapping=aes(fill=mpaa),   
      text_colors=c(
        on_background='brown', on_bar='yellow'))
    + annotate(
      geom='text', x=Inf, y=Inf,
      label=paste('Total:', nrow(movies)),
      vjust=1, hjust=1
    )
    +theme(plot.background=element_rect(fill='#E5D3B3'))),
  set_sizes=(
      upset_set_size()
      + theme(axis.text.x=element_text(angle=90))),
  min_size=10,
  stripes=c('cornsilk1', 'deepskyblue1'),
  width_ratio=0.1
)
image

交互的高亮顯示

set_size(8, 3)
upset(
  movies, c("Action", "Comedy", "Drama"),
  width_ratio=0.2,
  group_by='sets',
  queries=list(
    upset_query(
      intersect=c('Drama', 'Comedy'),
      color='red',
      fill='red',
      only_components=c('intersections_matrix', 'Intersection size')
    ),
    upset_query(group='Drama', color='blue'),
    upset_query(group='Comedy', color='orange'),
    upset_query(group='Action', color='purple'),
    upset_query(set='Drama', fill='blue'),
    upset_query(set='Comedy', fill='orange'),
    upset_query(set='Action', fill='purple')
  )
)
image

歡迎關(guān)注微信公眾號“生信小書生”,學(xué)習(xí)科研繪圖技巧。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容