将第二个轴标签添加到刻面图

时间:2015-04-10 21:01:11

标签: r ggplot2

如何在刻面图中添加第二个轴标签? 我意识到在大多数情况下它们应该是相同的,但我有一个顶行,其中包含不同模型的技能指标,而底行则是技能上的差异,所以我想在现有轴标签下面加bquote(Delta*.(costlong)),在第二排刻面的左侧。 我试过了

`labs(y=bquote(Delta*.(costlong)*"                      "*.(costlong)*"               "))+` 

但它不可能很好地居中并且在导出时会移动。

我也玩过

+annotation_custom(textGrob('bquote(Delta*.(costlong))),xmin=-20,xmax=-10,ymin=0,ymax=.2)

但它没有显示出来。我将x轴从0限制为160。

这里有一些数据和绘图代码:

costlong=bquote(r^2)
skill.m=data.frame(doy=seq(1,160),yr=2000:2001,variable=factor(c('skill_phvfull','skill_phvrcnfull','fulldiff'),levels=c('skill_phvfull','skill_phvrcnfull','fulldiff')),value=runif(2*3*160,0,.6))
skill.m$set=ifelse(grepl('skill',as.character(skill.m$variable)),'data','diff')
skill.m$set=as.factor(skill.m$set)
skill.m[grepl('diff',as.character(skill.m$variable)),'value']=skill.m[grepl('diff',as.character(skill.m$variable)),'value']/3
ggplot(skill.m)+      
     geom_point(aes(x=as.numeric(doy),y=value,colour=variable),alpha=.75,size=1)+
     facet_grid(set~yr,scale='free',space='free')+
     labs(y=costlong)+
     scale_colour_brewer(name='',palette='Set1',labels=c('PHV (blended)','PHV+RCN (blended)','Blended Skill Difference'))+
     scale_x_continuous('day of year',limits=c(1,160),labels=c(1,seq(30,160,30)),breaks=c(1,seq(30,160,30)))+
     scale_y_continuous(breaks=seq(0,.7,.1),labels=seq(0,.7,.1))+
     guides(colour=guide_legend(override.aes=list(alpha=1,size=2)))+
     theme_bw()+
     theme(axis.line=element_line(colour='grey10'),
           strip.background=element_rect(fill='white',colour='white'),
           strip.text=element_text(face='bold',size='12'),
           axis.text.x=element_text(size=8,angle=90,hjust=1,vjust=.5),
           axis.text.y=element_text(size=8,angle=0),
           legend.key=element_rect(colour='white'),
           legend.position = "bottom", 
           legend.box = "horizontal")

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

一种方法是使用gtable函数。使用空白y标签绘制ggplot。然后将ggplot转换为grob,为两个标签(上部组件和下部组件)构造两个文本grob,然后将文本grob插入到布局中。

library(ggplot2)
library(gtable)
library(grid)

costlong=bquote(r^2)
skill.m=data.frame(doy=seq(1,160),yr=2000:2001,variable=factor(c('skill_phvfull','skill_phvrcnfull','fulldiff'),levels=c('skill_phvfull','skill_phvrcnfull','fulldiff')),value=runif(2*3*160,0,.6))
skill.m$set=ifelse(grepl('skill',as.character(skill.m$variable)),'data','diff')
skill.m$set=as.factor(skill.m$set)
skill.m[grepl('diff',as.character(skill.m$variable)),'value']=skill.m[grepl('diff',as.character(skill.m$variable)),'value']/3

p = ggplot(skill.m)+      
     geom_point(aes(x=as.numeric(doy),y=value,colour=variable),alpha=.75,size=1)+
     facet_grid(set~yr,scale='free',space='free')+
     labs(y="")+
     scale_colour_brewer(name='',palette='Set1',labels=c('PHV (blended)','PHV+RCN (blended)','Blended Skill Difference'))+
     scale_x_continuous('day of year',limits=c(1,160),labels=c(1,seq(30,160,30)),breaks=c(1,seq(30,160,30)))+
     scale_y_continuous(breaks=seq(0,.7,.1),labels=seq(0,.7,.1))+
     guides(colour=guide_legend(override.aes=list(alpha=1,size=2)))+
     theme_bw()+
     theme(axis.line=element_line(colour='grey10'),
           strip.background=element_rect(fill=NA,colour=NA),
           strip.text=element_text(face='bold',size='12'),
           axis.text.x=element_text(size=8,angle=90,hjust=1,vjust=.5),
           axis.text.y=element_text(size=8,angle=0),
           legend.key=element_rect(colour='white'),
           legend.position = "bottom", 
           legend.box = "horizontal")

# Convert the plot to a grob
gt = ggplotGrob(p)

# Check the layout
gtable_show_layout(gt)  # To manually find the row and column for the labels

# Construct text grobs - one for each label
labL = textGrob(expression(bold(Delta * r^2)), rot = 90,
        gp = gpar(fontsize = 12))
labU = textGrob(expression(bold(r^2)), rot = 90,
        gp = gpar(fontsize = 12))

# Insert the text grobs into the layout
gt <- gtable_add_grob(gt, labL, t = 9, l =  2)
gt <- gtable_add_grob(gt, labU, t = 7, l =  2)

# Make the column a little wider
gt$widths[2] = unit(2, "lines")

# Draw it
grid.newpage()
grid.draw(gt)

enter image description here