How to dynamically wrap facet label using ggplot2

前端 未结 4 918
余生分开走
余生分开走 2020-12-14 15:52

I\'m looking for a way to dynamically wrap the strip label text in a facet_wrap or facet_grid call. I\'ve found a way to accomplish this using

4条回答
  •  失恋的感觉
    2020-12-14 16:15

    Thanks to the guidance from @baptiste and @thunk, I created the function below, which seems to do a pretty good job of automatically wrapping facet labels. Suggestions for improvement are always welcome, though.

    strwrap_strip_text = function(p, pad=0.05) { 
      # get facet font attributes
      th = theme_get()
      if (length(p$theme) > 0L)
        th = th + p$theme
    
      require("grid")
      grobs <- ggplotGrob(p)
    
      # wrap strip x text
      if ((class(p$facet)[1] == "grid" && !is.null(names(p$facet$cols))) ||
            class(p$facet)[1] == "wrap")
      {
        ps = calc_element("strip.text.x", th)[["size"]]
        family = calc_element("strip.text.x", th)[["family"]]
        face = calc_element("strip.text.x", th)[["face"]]
    
        if (class(p$facet)[1] == "wrap") {
          nm = names(p$facet$facets)
        } else {
          nm = names(p$facet$cols)
        }
    
        # get number of facet columns
        levs = levels(factor(p$data[[nm]]))
        npanels = length(levs)
        if (class(p$facet)[1] == "wrap") {
          cols = n2mfrow(npanels)[1]
        } else {
          cols = npanels
        }
    
        # get plot width
        sum = sum(sapply(grobs$width, function(x) convertWidth(x, "in")))
        panels_width = par("din")[1] - sum  # inches
        # determine strwrap width
        panel_width = panels_width / cols
        mx_ind = which.max(nchar(levs))
        char_width = strwidth(levs[mx_ind], units="inches", cex=ps / par("ps"), 
                              family=family, font=gpar(fontface=face)$font) / 
          nchar(levs[mx_ind])
        width = floor((panel_width - pad)/ char_width)  # characters
    
        # wrap facet text
        p$data[[nm]] = unlist(lapply(strwrap(p$data[[nm]], width=width, 
                                             simplify=FALSE), paste, collapse="\n"))
      }
    
      if (class(p$facet)[1] == "grid" && !is.null(names(p$facet$rows))) {  
        ps = calc_element("strip.text.y", th)[["size"]]
        family = calc_element("strip.text.y", th)[["family"]]
        face = calc_element("strip.text.y", th)[["face"]]
    
        nm = names(p$facet$rows)
    
        # get number of facet columns
        levs = levels(factor(p$data[[nm]]))
        rows = length(levs)
    
        # get plot height
        sum = sum(sapply(grobs$height, function(x) convertWidth(x, "in")))
        panels_height = par("din")[2] - sum  # inches
        # determine strwrap width
        panels_height = panels_height / rows
        mx_ind = which.max(nchar(levs))
        char_height = strwidth(levs[mx_ind], units="inches", cex=ps / par("ps"), 
                               family=family, font=gpar(fontface=face)$font) / 
          nchar(levs[mx_ind])
        width = floor((panels_height - pad)/ char_height)  # characters
    
        # wrap facet text
        p$data[[nm]] = unlist(lapply(strwrap(p$data[[nm]], width=width, 
                                             simplify=FALSE), paste, collapse="\n"))
      }
    
      invisible(p)
    }
    

    To use the function, call it in place of print.

    library(ggplot2)
    df = expand.grid(group=paste(c("Very Very Very Long Group Name "), 1:4),
                     group1=paste(c("Very Very Very Long Group Name "), 5:8),
                     x=rnorm(5), y=rnorm(5), stringsAsFactors=FALSE)
    
    p = ggplot(df) +
      geom_point(aes(x=x, y=y)) +
      facet_grid(group1~group)
    strwrap_strip_text(p)
    

提交回复
热议问题