How to use different font sizes in ggplot facet wrap labels?

一个人想着一个人 提交于 2019-11-29 09:53:34

The solution below is a hack in that it uses a superscript (or subscript) to get a smaller font size for the second line of the facet label. I'm not sure how to get finer control of the label size without resorting to direct manipulation of the strip grobs, though there might be a way to write a labeller function to do it.

We'll use the built-in mtcars data frame for the example. First, we'll add columns that will be used both for facetting and labeling. We'll facet by cyl, but we want the first line of the label to display the number of cylinders for that facet and the second line to display the number of data points in that facet. To do this, we'll create two new columns in mtcars, called Label1 and Label2 that we'll use to create each line of the facet label. We'll facet by these two columns to get the labels we want in the graph. (Label3 is similar to Label2, but uses subscript instead of superscript; this only matters if you want to change the distance from the bottom of the second line to the top of the plot panel.)

Label1 and Label2 are text strings, but they're in the form of expressions so that we can use label_parsed to get smaller subscripted text when we create the plot.

library(ggplot2)
library(dplyr)
library(grid)

mtcars.new = mtcars %>% group_by(cyl) %>% 
  summarise(Label1=paste0("bold(Cylinders:~", unique(cyl),")"),
            Label2=paste0("bold(NULL[Count:~", length(cyl),"])"),
            Label3=paste0("bold(NULL^{Count:~", length(cyl),"})")) %>%
  full_join(mtcars)

Now we can create the plot. Facetting by Label1 and Label2 gives us two lines. Because we created a subscripted expression for Label2, this gets rendered in a smaller font when we use label_parsed to label the facets. I'd prefer the Label2 to be a bit larger, relative to Label1, but there's no way to control that with this (hacky) method. Also, although element_text has a lineheight argument, ggplot doesn't seem to be respecting it. As a result, I've manually reset the lineheight for the strip labels to reduce the space between the two labels.

p = ggplot(mtcars.new, aes(wt, mpg)) +
  geom_point() +
  facet_grid(. ~ Label1 + Label2, labeller=label_parsed) +
  theme_bw() +
  theme(strip.background=element_rect(fill=NA, color=NA),
        strip.text=element_text(size=12))

g <- ggplotGrob(p)
g$heights[[3]] = unit(0.5,"lines")

grid.draw(g)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!