Resize Facet Shortcuts

I know the question was asked here: Is there a way to increase the height of the strip strip.text in the face?

I want to reduce strip.text strip height without resizing text. In the current case, there is always space between the text and the wall stripes.

Here is what I have tried so far,

library(gcookbook) # For the 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 my case, it seems that lineheight does not affect anything, even if it is changed to 5 . Why?
How can I make the cut bar size a little smaller, but the text size is the same?

enter image description here

change after answer @Sandy Muspratt

we can reduce the size of the strip if there is only one line of facets .

 g = ggplotGrob(p) g$heights[c(3)] = unit(.4, "cm") # Set the height grid.newpage() grid.draw(g) 

enter image description here

However, in my real data, I have many lines of the graph, as shown below, and when I changed the 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)) 

enter image description here

  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 I tried to change the 1,7 and 11 elements

 g$heights[c(3,7,11)] = unit(.4, "cm") # Set the height grid.newpage() grid.draw(g) 

enter image description here

Without resizing facet labels.

 > 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 
+8
r ggplot2 facet-wrap
source share
1 answer

Use fields

From about ggplot2 ver 2.1.0: In theme specify the fields in the strip_text element (see here ).

 library(ggplot2) library(gcookbook) # For the 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"))) 



Original answer updated to ggplot2 v2.2.0

Your facet_grid chart

This will reduce the height of the strip (if you want) to zero height. The height should be set for one strip and three gnomes. This will work with your specific facet_grid example.

 library(ggplot2) library(grid) library(gtable) library(gcookbook) # For the 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 the 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 are three stripes on the page. Therefore, you must change the three heights of the strip and change the three heights of the block.

The following will work with your 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") # Three strip heights changed for(i in c(17,18,19)) g$grobs[[i]]$heights <- unit(1, "npc") # The height of three grobs changed grid.newpage() grid.draw(g) 

How to find appropriate heights and rodents?

g$heights returns the height vector. 1st height are story panels. The height of the strip is one earlier - it is 6, 11, 16.

g$layout returns a data frame with the names of the gnomes in the last column. Rodents that need their heights change with names starting with a โ€œstripeโ€. They are in lines 17, 18, 19.

To generalize a 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) # The heights that need changing are in positions one less than the 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") # The grobs that need their 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

Almost the same code can be used, even with a title and legend located on top. There is a change in the calculation of pos , but even without this change, the code is executed.

 library(ggplot2) library(grid) # Some data df = data.frame(x= rnorm(100), y = rnorm(100), z = sample(1:12, 100, T), col = sample(c("a","b"), 100, T)) # The 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) # The heights that need changing are in positions one less than the 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") # The grobs that need their 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) 
+10
source share

All Articles