How to add a common label to faces in ggplot2?

I often have numerical values ​​for cutting. I want to provide enough information to interpret these cut values ​​in an additional header, similar to axis names. The labeller parameters repeat a lot of unnecessary text and are not suitable for longer variable headers.

Any suggestions?

Default:

test<-data.frame(x=1:20, y=21:40, facet.a=rep(c(1,2),10), facet.b=rep(c(1,2), each=20)) qplot(data=test, x=x, y=y, facets=facet.b~facet.a) 

enter image description here

What I would like:

enter image description here

The best I can do in ggplot:

 qplot(data=test, x=x, y=y)+facet_grid(facet.b~facet.a, labeller=label_both) 

enter image description here

As @Hendy pointed out, it looks like: add a secondary y axis to the ggplot2 plots - make it perfect

+69
r ggplot2 label facet
Jul 05 '12 at 22:15
source share
3 answers

Since the latter ggplot2 uses gtable internally, it is fairly easy to change the shape:

 library(ggplot2) test <- data.frame(x=1:20, y=21:40, facet.a=rep(c(1,2),10), facet.b=rep(c(1,2), each=20)) p <- qplot(data=test, x=x, y=y, facets=facet.b~facet.a) # get gtable object z <- ggplotGrob(p) library(grid) library(gtable) # add label for right strip z <- gtable_add_cols(z, unit(z$widths[[7]], 'cm'), 7) z <- gtable_add_grob(z, list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))), textGrob("Variable 1", rot = -90, gp = gpar(col = gray(1)))), 4, 8, 6, name = paste(runif(2))) # add label for top strip z <- gtable_add_rows(z, unit(z$heights[[3]], 'cm'), 2) z <- gtable_add_grob(z, list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))), textGrob("Variable 2", gp = gpar(col = gray(1)))), 3, 4, 3, 6, name = paste(runif(2))) # add margins z <- gtable_add_cols(z, unit(1/8, "line"), 7) z <- gtable_add_rows(z, unit(1/8, "line"), 3) # draw it grid.newpage() grid.draw(z) 

enter image description here

Of course, you can write a function that automatically adds tag labels. A future version of ggplot2 may have this functionality; not sure though.

+44
Sep 30 2018-12-12T00: 00Z
source share

In addition to the method described by kohske, you can add a border to the fields added by changing

 col=NA 

to

 col=gray(0.5), linetype=1 

Also change

 fill=gray(0.5) 

for

 fill=grey(0.8) 

and

 gp=gpar(col=gray(1)) 

to

 gp=gpar(col=gray(0)) 

If you want new columns to match face labels

t

 z <- gtable_add_grob(z, list(rectGrob(gp = gpar(col = gray(0.5), linetype=1, fill = gray(0.8))), textGrob("Variable 1", rot = -90, gp = gpar(col = gray(0)))), 4, 8, 6, name = paste(runif(2))) 
+1
Aug 21 '14 at 2:24 on
source share

There may be a better way to do this, but you can:

 fac1 = factor(rep(c('a','b'),10)) fac2 = factor(rep(c('a','b'),10)) data = data.frame(x=1:10, y=1:10, fac1=fac1, fac2=fac2) p = ggplot(data,aes(x,y)) + ggplot2::geom_point() + facet_grid(fac1~fac2) p + theme(plot.margin = unit(c(1.5,1.5,0.2,0.2), "cm")) grid::grid.text(unit(0.98,"npc"),0.5,label = 'label ar right', rot = 270) # right grid::grid.text(unit(0.5,"npc"),unit(.98,'npc'),label = 'label at top', rot = 0) # top 
0
Dec 29 '17 at 15:36
source share



All Articles