R繪圖的生物學(xué)家(7): Multifaceted figures

本節(jié)講的是如何多元化展示圖片。

代碼

1. 數(shù)據(jù)導(dǎo)入及整理

這一部分依舊是之前的內(nèi)容,不做過(guò)多的解釋
library(ggplot2)
# reset theme
theme_set(theme_gray())

# Loading the data
filename <- "Lesson-07/variants_from_assembly.bed"
my_data <- read.csv(filename, sep="\t", quote='', stringsAsFactors=TRUE,header=FALSE)
names(my_data) <- c("chrom","start","stop","name","size","strand","type","ref.dist","query.dist")

head(my_data)

# Filtering and polishing data
my_data <- my_data[my_data$chrom %in% c(seq(1,22),"X","Y","MT"),]

    # ordering chromosomes
my_data$chrom <- factor(gsub("chr", "", my_data$chrom), levels=c(seq(1,22),"X","Y","MT"))

    # ordering types
my_data$type <- factor(my_data$type, levels=c("Insertion","Deletion","Expansion","Contraction"))

2. 多元化作圖

# 核密度圖的繪制
ggplot(my_data, aes(x=size,fill=type)) + geom_density(alpha=0.5) + xlim(0,500) 
# 橫向標(biāo)簽為type,這里facet_grid參數(shù)是新的,用來(lái)多元化作圖,y軸表示沒有標(biāo)簽
ggplot(my_data, aes(x=size,fill=type)) + geom_density() + xlim(0,500)  + facet_grid(type ~ .)
# 只是將標(biāo)簽換到y(tǒng)軸
ggplot(my_data, aes(x=size,fill=type)) + geom_density() + xlim(0,500)  + facet_grid(. ~ type)
# facet_grid的語(yǔ)法(行~列)
# plot + facet_grid(rows ~ columns)

# Facet on type and chrom
ggplot(my_data, aes(x=size,fill=type)) + geom_density() + xlim(0,500)  + facet_grid(chrom ~ type)

ggplot(my_data, aes(x=size,fill=type)) + geom_density() + xlim(0,500)  + facet_grid(type ~ chrom)

# bar圖
ggplot(my_data, aes(x=size,fill=type)) + geom_bar() + xlim(0,500)  + facet_grid(chrom ~ type)
# 箱線圖
ggplot(my_data, aes(x=type,y=size,color=type,fill=type)) + geom_boxplot() + facet_grid(chrom ~ .)
# 小提琴圖
ggplot(my_data, aes(x=type,y=size,color=type,fill=type)) + geom_violin() + facet_grid(chrom ~ .)
# 幾何點(diǎn)
ggplot(my_data, aes(x=ref.dist,y=query.dist,color=type,fill=type)) + xlim(0,500) + ylim(0,500) + geom_point() + facet_grid(chrom ~ type)
# dotplot圖
ggplot(my_data, aes(x=size,fill=type)) + geom_dotplot() + xlim(5000,10000) + facet_grid(chrom ~ type)
原始的核密度圖
標(biāo)簽在右側(cè)
標(biāo)簽在頂端
chrom~type
type~chrom
bar圖的type~chrom
箱線圖的chrom~.
小提琴圖
幾何點(diǎn)圖
dotplot

3.內(nèi)置圖片

# Inset figures:
#使用第五節(jié)課的圖片參數(shù)
theme_set(theme_gray() + 
              theme(
                  axis.line = element_line(size=0.5),
                  panel.background = element_rect(fill=NA,size=rel(20)), 
                  panel.grid.minor = element_line(colour = NA), 
                  axis.text = element_text(size=16), 
                  axis.title = element_text(size=18)
                  )
          )
# 大圖的繪制
big_plot <-  ggplot(my_data, aes(x=size,fill=type)) + 
    geom_bar(binwidth=100) +  
    guides(fill=FALSE) + 
    scale_y_continuous(expand=c(0,0)) # Move bars down to X-axis

big_plot
# 小圖繪制
small_plot <- ggplot(my_data, aes(x=size,fill=type)) + geom_bar(binwidth=5) + xlim(0,500) + theme(axis.title=element_blank()) +  scale_y_continuous(expand=c(0,0))
small_plot

# 在大圖中插入小圖
library(grid)
vp <- viewport(width = 0.8, height = 0.7, x = 0.65, y = 0.65)
                # width, height, x-position, y-position of the smaller plot

png("Lesson-07/inset_plot.png")
print(big_plot)
print(small_plot, vp = vp)
dev.off()

圖中圖繪制

從上星期一直拖到了現(xiàn)在,加上現(xiàn)在有些感冒,真的有點(diǎn)暈了,果然還是需要有連續(xù)性。很多地方需要繼續(xù)補(bǔ)充說(shuō)明,現(xiàn)在也只是大致的學(xué)習(xí)一遍。

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

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

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