Facetters in facet panel invade against panel background

I have a plot on a white background with gray edges and white background text:

ggplot(data = data.frame(x = rep(1:2, 2), y = rep(1:2,2), color = c("Ap", "Ap", "B", "B")), 
       aes(x = x, y = y, color = color)) + 
  geom_point() + 
  facet_grid(~color) + 
  theme(panel.background = element_blank(), 
        strip.text = element_text(color = "white", size = 23)) 

enter image description here

My problem is that descenders (p, g, q, j) cross the face. I would like the background of the strip to have a marker around the text, so that all the glyphs in the facet text are always strictly in the rectangle of the face. I can add newline characters to the facet text color = c("Ap\n", "Ap\n", "B\n", "B\n"), but the edge is too excessive (or the required one is lineheighttoo ugly). Is there a solution ggplot2for this?

+4
source share
1 answer

For ggplot v2.2.0 V themespecify the fields in the element strip_text(see here )

# Set text size
size = 26

library(ggplot2)
library(grid)

p = ggplot(data = data.frame(x = rep(1:2, 2), y = rep(1:2,2), color = c("Ap", "Ap", "B", "B")), 
      aes(x = x, y = y, color = color)) + 
      geom_point() + 
      facet_grid(~color) + theme_bw() +
      theme(strip.text = element_text(color = "white", size = size)) 


  p +
  theme(strip.text.x = element_text(margin = margin(.1, 0, .3, 0, "cm")))

ggplot . ​​ , , unit(1, "cm"), , , , .

: ggplot2 2.0.0
: 3.0.0 grid:::unit.list() .

# Set text size
size = 26

library(ggplot2)
library(grid)

p = ggplot(data = data.frame(x = rep(1:2, 2), y = rep(1:2,2), color = c("Ap", "Ap", "B", "B")), 
      aes(x = x, y = y, color = color)) + 
      geom_point() + 
      facet_grid(~color) + theme_bw() +
      theme(strip.text = element_text(color = "white", size = size)) 


# Get ggplot grob
g <- ggplotGrob(p)

# Set the relevant height
g$heights[3] = unit(2, "grobheight", textGrob("a", gp=gpar(fontsize = size))) 


grid.newpage()
grid.draw(g)

enter image description here

+5

All Articles