2016-08-01 55 views
0

我試圖用的包使用grid.arrange繪製三個數字。我希望它們在一行中顯示爲3列,其中最左邊的圖應該有y軸,但沒有圖例,中間圖沒有y軸並且沒有圖例,最右邊的圖應該沒有y軸,但是應該包括傳說。這樣,圖例和y軸與所有圖形相同,只出現一次。在grid.arrange中獲取均勻的圖形寬度

下面是數據 - 它們與基因本體論的富集測試:

首先,圖例的顏色方案 - 爲每個富集p值範圍內的顏色:

color.order <- c("#7d4343","#B20000","#C74747","#E09898","#EBCCD6","#C8C8C8") 
names(color.order) <- c("(0-0.05]","(0.05-0.1]","(0.1-0.15]","(0.15-0.2]","(0.2-0.25]","(0.25-1]") 

然後圖data.frame S:

df.g1 <- data.frame(category=c("C1-up","C1-down","C2-up","C2-down"), 
       p.value=c(0.4833,0.5114,0.3487,0.6522),log10.p.value=c(3.157832,2.912393,4.575481,1.856192), 
       col=c("(0.25-1]","(0.25-1]","(0.25-1]","(0.25-1]"), 
       col.cat=c("(0.25-1]","(0.25-1]","(0.25-1]","(0.25-1]")) 
df.g2 <- data.frame(category=c("C1-up","C1-down","C2-up","C2-down"), 
        p.value=c(0.5345,0.4819,0.9986,0.0013),log10.p.value=c(2.720522905,3.170430737,0.006084383,28.860566477), 
        col=c("(0.25-1]","(0.25-1]","(0.25-1]","(0-0.05]"), 
        col.cat=c("(0.25-1]","(0.25-1]","(0.25-1]","(0-0.05]")) 
df.g3 <- data.frame(category=c("C1-up","C1-down","C2-up","C2-down"), 
        p.value=c(0.2262,0.7703,0.9926,0.0080),log10.p.value=c(6.45507399,1.13340102,0.03225729,20.96910013), 
        col=c("(0.2-0.25]","(0.25-1]","(0.25-1]","(0-0.05]"), 
        col.cat=c("(0.2-0.25]","(0.25-1]","(0.25-1]","(0-0.05]")) 

把它們放在一起在一個列表:

df.list <- list(g1=df.g1,g2=df.g2,g3=df.g3) 

這對於關聯p值的傳說範圍顏色:

color.order <- c("#7d4343","#B20000","#C74747","#E09898","#EBCCD6","#C8C8C8") 
names(color.order) <- c("(0-0.05]","(0.05-0.1]","(0.1-0.15]","(0.15-0.2]","(0.2-0.25]","(0.25-1]") 

和劇情創作代碼:

library(ggplot2) 
library(gridExtra) 

ggplot.list <- vector(mode="list", length(df.list)) 
for(g in 1:length(df.list)) 
{ 
    if(g==1){ #draw y-axis but no legend 
    ggplot.list[[g]] <- ggplot(df.list[[g]], aes(y=log10.p.value,x=category,fill=col))+ 
     scale_fill_manual(drop=FALSE,values=color.order,name="Enrichment P-value",guide=F)+ 
     geom_bar(stat="identity",width=0.2)+scale_y_continuous(limits=c(0,30),labels=c(seq(0,20,10)," >30"),expand=c(0,0))+ 
     theme_bw()+theme(panel.border=element_blank(),axis.text=element_text(size=8),axis.title=element_text(size=8,face="bold"))+coord_flip()+theme(plot.margin=unit(c(0.1,1,0.1,0.1),"cm"),axis.title.y = element_text(size=8),axis.title.x = element_text(size=8))+labs(x="Category",y="-10log10(P-value)")+ggtitle(names(df.list)[g]) 
    } else if(g==2){ #no y-axis and no legend 
    ggplot.list[[g]] <- ggplot(df.list[[g]], aes(y=log10.p.value,x=category,fill=col))+ 
     scale_fill_manual(drop=FALSE,values=color.order,name="Enrichment P-value",guide=F)+ 
     geom_bar(stat="identity",width=0.2)+scale_y_continuous(limits=c(0,30),labels = c(seq(0,20,10)," >30"),expand=c(0,0))+ 
     theme_bw()+theme(panel.border=element_blank(),axis.text=element_text(size=8),axis.title=element_text(size=8,face="bold"))+coord_flip()+theme(plot.margin=unit(c(0.1,1,0.1,0.1),"cm"),axis.title.y = element_blank(),axis.text.y=element_blank(),axis.title.x = element_text(size=8))+labs(y="-10log10(P-value)")+ggtitle(names(df.list)[g]) 
    } else if(g==3){ #only legend 
    ggplot.list[[g]] <- ggplot(df.list[[g]], aes(y=log10.p.value,x=category,fill=col))+ 
     scale_fill_manual(drop=FALSE,values=color.order,name="Enrichment P-value")+ 
     geom_bar(stat="identity",width=0.2)+scale_y_continuous(limits=c(0,30),labels = c(seq(0,20,10)," >30"),expand=c(0,0))+ 
     theme_bw()+theme(panel.border=element_blank(),axis.text=element_text(size=8),axis.title=element_text(size=8,face="bold"))+coord_flip()+theme(plot.margin=unit(c(0.1,1,0.1,0.1),"cm"),axis.title.y = element_blank(),axis.text.y=element_blank(),axis.title.x = element_text(size=8))+labs(y="-10log10(P-value)")+ggtitle(names(df.list)[g]) 
    } 
} 

這讓我幾乎什麼,我需要: enter image description here

我的問題是這三個數字有不同的寬度。所以我的問題是如何使寬度相同?

回答

1

這個數據似乎量身定做刻面:

library(dplyr) 
library(ggplot2) 

color.order <- c("#7d4343","#B20000","#C74747","#E09898","#EBCCD6","#C8C8C8") 
names(color.order) <- c("(0-0.05]","(0.05-0.1]","(0.1-0.15]","(0.15-0.2]","(0.2-0.25]","(0.25-1]") 

df <- bind_rows(df.list, .id="grp") 
df <- mutate(df, col=factor(col, levels=names(color.order))) 

gg <- ggplot(df, aes(y=log10.p.value, x=category, fill=col)) 
gg <- gg + geom_bar(stat="identity", width=0.2) 
gg <- gg + scale_y_continuous(limits=c(0,30), labels=c(seq(0,20,10)," >30"), expand=c(0,0)) 
gg <- gg + scale_fill_manual(drop=FALSE, values=color.order, name="Enrichment P-value") 
gg <- gg + coord_flip() 
gg <- gg + facet_wrap(~grp) 
gg <- gg + labs(x="Category", y="-10log10(P-value)") 
gg <- gg + theme_bw() 
gg <- gg + theme(panel.border=element_blank(), 
       panel.margin=margin(1,1,1,1, unit="cm"), 
       axis.text=element_text(size=8), 
       axis.title=element_text(size=8,face="bold"), 
       axis.title.y=element_text(size=8), 
       axis.title.x=element_text(size=8), 
       strip.background=element_blank(), 
       plot.margin=margin(0.1, 0.1, 0.1, 0.1, unit="cm")) 
gg 

enter image description here

相關問題