【问题标题】:Getting even figure widths in grid.arrange在 grid.arrange 中获得均匀的图形宽度
【发布时间】:2016-08-01 16:57:05
【问题描述】:

我正在尝试使用 R'sgridExtra 包中的 grid.arrange 绘制三个数字。我希望它们在一行中显示为 3 列,其中最左边的图应该有 y 轴但没有图例,中间图没有 y 轴也没有图例,最右边的图应该没有 y 轴但是应该包括图例。这样,与所有图形相同的图例和 y 轴只出现一次。

以下是数据 - 它们与基因本体富集测试有关:

首先,图例的配色方案——每个富集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.frames:

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])
  }
}

这给了我几乎我需要的东西:

我的问题是这三个数字的宽度不同。所以我的问题是如何使宽度相同?

【问题讨论】:

    标签: r ggplot2 gridextra


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2013-04-15
      • 2019-06-28
      • 2012-07-05
      • 2016-05-02
      • 1970-01-01
      • 2012-09-29
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多