add second axis label

by Dominik   Last Updated April 10, 2015 23:11 PM - source

How does one add a second axis label to facetted plots? I realize in most cases they should be the same, but I have a top row with skill metrics for different models while the bottom row is the difference in skill so I'd like to include bquote(Delta*.(costlong)) below the existing axis label, and to the left of the 2nd row of facets. I've tried

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

but its impossible to center well and it moves when exporting.

I've also played with

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

but it doesn't show up. I am limiting my x axis from 0 to 160.

here is some data and plotting code:

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")

any suggestions?

Tags : r ggplot2


Answers 1


One way is to use gtable functions. Draw the ggplot with a blank y label. Then convert the ggplot to a grob, construct two text grobs for the two labels (the upper component and the lower component), then insert the text grobs into the layout.

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 = 6, l =  2)
gt <- gtable_add_grob(gt, labU, t = 4, 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

Sandy Muspratt
Sandy Muspratt
April 15, 2015 01:03 AM

Related Questions


R - How to find points within specific Contour

Updated May 28, 2015 23:11 PM

How to see the code of a stored plot (ggplot)

Updated May 29, 2015 01:11 AM

R ggplot remove certain items from legend

Updated April 03, 2015 23:11 PM

Error in facet_grid in ggplot2

Updated October 09, 2016 09:11 AM