r - Changing the Appearance of Facet Labels size -
i know question asked here: is there way increase height of strip.text bar in facet?
i want decrease height of strip.text bar without changing text size. in current case there space left between text , strip bar walls.
here tried far,
library(gcookbook) # data set library(ggplot2) ggplot(cabbage_exp, aes(x=cultivar, y=weight)) + geom_bar(stat="identity") + facet_grid(.~ date) + theme(strip.text = element_text(face="bold", size=9,lineheight=5.0), strip.background = element_rect(fill="lightblue", colour="black", size=1))
in case seems lineheight
not affect if changed 5
. why?
how can make strip bar size little smaller keeping text size same?
edit after @sandy muspratt answer
we able reduce strip size if there 1 row of facets
.
g = ggplotgrob(p) g$heights[c(3)] = unit(.4, "cm") # set height grid.newpage() grid.draw(g)
however, in real data have many rows of plot below , when changed elements of g$heights nothing happened!
p = ggplot(cabbage_exp, aes(x=cultivar, y=weight)) + geom_bar(stat="identity") + facet_wrap(~ date,ncol = 1) + theme(strip.text = element_text(face="bold", size=9), strip.background = element_rect(fill="lightblue", colour="black",size=1))
g = ggplotgrob(p) g$heights # [1] 5.5pt 0cm 0.66882800608828cm #1null 0cm 0.193302891933029cm # [7] 0.66882800608828cm 1null 0cm #0.193302891933029cm 0.66882800608828cm 1null # [13] 0.456194824961948cm 0cm 1grobheight 5.5pt
then attempted change 1,7 , 11
elements
g$heights[c(3,7,11)] = unit(.4, "cm") # set height grid.newpage() grid.draw(g)
no change in facet label size.
> g$heights [1] 5.5pt 1grobheight [3] sum(0.2cm, sum(0.15cm, 0.8128cm, 0cm, 0.15cm), 0.2cm)+0.2cm 0.2 [5] 1null 0cm [7] 0.193302891933029cm 0.2 [9] 1null 0cm [11] 0.193302891933029cm 0.2 [13] 1null 0cm [15] 0.193302891933029cm 0.2 [17] 1null 0.456194824961948cm [19] 0cm 1grobheight [21] 5.5pt
use margins
from ggplot2 ver 2.1.0: in theme
, specify margins in strip_text
element (see here).
library(ggplot2) library(gcookbook) # data set p = ggplot(cabbage_exp, aes(x=cultivar, y=weight)) + geom_bar(stat="identity") + facet_grid(. ~ date) + theme(strip.text = element_text(face="bold", size=9), strip.background = element_rect(fill="lightblue", colour="black",size=1)) p + theme(strip.text.x = element_text(margin = margin(.1, 0, .1, 0, "cm")))
the original answer updated ggplot2 v2.2.0
your facet_grid chart
this reduce height of strip (all way 0 height if want). height needs set 1 strip , 3 grobs. work specific facet_grid example.
library(ggplot2) library(grid) library(gtable) library(gcookbook) # data set p = ggplot(cabbage_exp, aes(x=cultivar, y=weight)) + geom_bar(stat="identity") + facet_grid(. ~ date) + theme(strip.text = element_text(face="bold", size=9), strip.background = element_rect(fill="lightblue", colour="black",size=1)) g = ggplotgrob(p) g$heights[6] = unit(0.4, "cm") # set height for(i in 13:15) g$grobs[[i]]$heights = unit(1, "npc") # set height of grobs grid.newpage() grid.draw(g)
your facet_wrap chart
there 3 strips down page. therefore, there 3 strip heights changed, , 3 grob heights changed.
the following work specific facet_wrap example.
p = ggplot(cabbage_exp, aes(x=cultivar, y=weight)) + geom_bar(stat="identity") + facet_wrap(~ date,ncol = 1) + theme(strip.text = element_text(face="bold", size=9), strip.background = element_rect(fill="lightblue", colour="black",size=1)) g = ggplotgrob(p) for(i in c(6,11,16)) g$heights[[i]] = unit(0.4,"cm") # 3 strip heights changed for(i in c(17,18,19)) g$grobs[[i]]$heights <- unit(1, "npc") # height of 3 grobs changed grid.newpage() grid.draw(g)
how find relevant heights , grobs?
g$heights
returns vector of heights. 1null heights plot panels. strip heights 1 before - 6, 11, 16.
g$layout
returns data frame names of grobs in last column. grobs need heights changed names beginning "strip". in rows 17, 18, 19.
to generalise little
p = ggplot(cabbage_exp, aes(x=cultivar, y=weight)) + geom_bar(stat="identity") + facet_wrap(~ date,ncol = 1) + theme(strip.text = element_text(face="bold", size=9), strip.background = element_rect(fill="lightblue", colour="black",size=1)) g = ggplotgrob(p) # heights need changing in positions 1 less plot panels pos = c(subset(g$layout, grepl("panel", g$layout$name), select = t)) for(i in pos) g$heights[i-1] = unit(0.4,"cm") # grobs need heights changed: grobs = which(grepl("strip", g$layout$name)) for(i in grobs) g$grobs[[i]]$heights <- unit(1, "npc") grid.newpage() grid.draw(g)
multiple panels per row
nearly same code can used, title , legend positioned on top. there change in calculation of pos
, without change, code runs.
library(ggplot2) library(grid) # data df = data.frame(x= rnorm(100), y = rnorm(100), z = sample(1:12, 100, t), col = sample(c("a","b"), 100, t)) # plot p = ggplot(df, aes(x = x, y = y, colour = col)) + geom_point() + labs(title = "made-up data") + facet_wrap(~ z, nrow = 4) + theme(legend.position = "top") g = ggplotgrob(p) # heights need changing in positions 1 less plot panels pos = c(unique(subset(g$layout, grepl("panel", g$layout$name), select = t))) for(i in pos) g$heights[i-1] = unit(0.2, "cm") # grobs need heights changed: grobs = which(grepl("strip", g$layout$name)) for(i in grobs) g$grobs[[i]]$heights <- unit(1, "npc") grid.newpage() grid.draw(g)
Comments
Post a Comment