2016-08-01 65 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

相关问题