我试图使用grid.arrange
R
gridExtra
个套餐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]")
来绘制三个数字。我希望它们在一行中显示为3列,其中最左边的图形应该具有y轴但没有图例,中间的图形没有y轴而没有图例,而最右边的图形应该没有y轴但是应该包括传说。这样,与所有数字相同的图例和y轴只出现一次。
以下是数据 - 它们与基因本体富集测试有关:
首先,图例的颜色方案 - 每个浓缩p值范围的颜色:
data.frame
然后是图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]"))
s:
df.list <- list(g1=df.g1,g2=df.g2,g3=df.g3)
将它们放在一个列表中:
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]")
这是将p值范围与颜色相关联的图例:
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])
}
}
剧情创作代码:
@media screen and (width: 200px){
#slider{display:none; !important;}
}
#main-menu-links a
{
background: #E37222;
}
/************
for slider
*****************/
/* http://www.menucool.com */
#sliderFrame, #sliderFrame div {
box-sizing: content-box;
}
#sliderFrame
{
width:600px;
min-width: 50px;
margin:0 auto; /*center-aligned*/
}
#slider, #slider .sliderInner {
width:600px;height:306px;/* Must be the same size as the slider images */
min-width:50px;
border-radius:3px;
}
#slider {
background-color:white;
position:relative;
margin:0 auto; /*center-aligned*/
transform: translate3d(0,0,0);
box-shadow: 0px 1px 5px #999999;
}
/* the link style (if an image is wrapped by a link) */
#slider a.imgLink, #slider .video {
z-index:2;
cursor:pointer;
position:absolute;
top:0px;left:0px;border:0;padding:0;margin:0;
width:100%;height:100%;
}
#slider .video {
background:transparent url(video.png) no-repeat 50% 50%;
}
/* Caption styles */
#slider .mc-caption {
position:absolute;
width:100%;
height:auto;
padding:10px 0;/* 10px will increase the height.*/
left:0px;
bottom:0px;top:auto;
overflow:hidden;
font: bold 14px/20px Arial;
color:#EEE;
text-align:center;
background:rgba(0,0,0,0.3);
}
#slider .mc-caption a {
color:#FB0;
}
#slider .mc-caption a:hover {
color:#DA0;
}
/* ------ built-in navigation bullets wrapper ------*/
#slider .navBulletsWrapper {
top:320px;
text-align:center;
background:none;
position:relative;
z-index:3;
}
/* each bullet */
#slider .navBulletsWrapper div
{
width:11px; height:11px;
font-size:0;color:White;/*hide the index number in the bullet element*/
background:transparent url(bullet.png) no-repeat 0 0;
display:inline-block; *display:inline; zoom:1;
overflow:hidden;cursor:pointer;
margin:0 6px;/* set distance between each bullet*/
}
#slider .navBulletsWrapper .active {background-position:0 -11px;}
/* --------- Others ------- */
#slider img, #slider>b, #slider a>b {
position:absolute; border:none; display:none;
}
#slider .sliderInner {
overflow:hidden;
position:absolute; top:0; left:0;
}
#slider>a, #slider video, #slider audio {display:none;}
#slider div {-webkit-transform: translate3d(0,0,0);transform: translate3d(0,0,0);}
我的问题是这三个数字有不同的宽度。所以我的问题是如何使宽度相同?
答案 0 :(得分: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