r - Descenders in facet panel intrude on panel background -
i have plot on white background grey facets , white facet 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)) 
my problem descenders (p, g, q, j) cross facet. strip background have margin around text glyphs in facet text strictly within facet rectangle. can add newlines facet text color = c("ap\n", "ap\n", "b\n", "b\n") margin excessive (or lineheight required ugly). there ggplot2 solution this?
for ggplot v2.2.0 in theme, specify margins in strip_text element (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"))) original use ggplot layout adjust height of strip. height set absolute height, instance, unit(1, "cm"), or, i've done here, set height adjusts font size.
edit: updating ggplot2 2.0.0
further edit: updating grid 3.0.0 grid:::unit.list() no longer needed.
# 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)) # ggplot grob g <- ggplotgrob(p) # set relevant height g$heights[3] = unit(2, "grobheight", textgrob("a", gp=gpar(fontsize = size))) grid.newpage() grid.draw(g) 
Comments
Post a Comment