Center-align legend title and legend keys in ggplot2 for long legend titles

后端 未结 3 762
既然无缘
既然无缘 2020-12-14 11:13

I am having a hard time making the title of a legend center-aligned relative to the legend keys when the legend title is long. There is a question from a year ago that works

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 11:46

    Update Oct. 4, 2019:

    A while back I wrote a fairly general function based on the original idea I posted here almost two years ago. The function is on github here but it's not part of any officially published package. It is defined as follows:

    align_legend <- function(p, hjust = 0.5)
    {
      # extract legend
      g <- cowplot::plot_to_gtable(p)
      grobs <- g$grobs
      legend_index <- which(sapply(grobs, function(x) x$name) == "guide-box")
      legend <- grobs[[legend_index]]
    
      # extract guides table
      guides_index <- which(sapply(legend$grobs, function(x) x$name) == "layout")
    
      # there can be multiple guides within one legend box  
      for (gi in guides_index) {
        guides <- legend$grobs[[gi]]
    
        # add extra column for spacing
        # guides$width[5] is the extra spacing from the end of the legend text
        # to the end of the legend title. If we instead distribute it by `hjust:(1-hjust)` on
        # both sides, we get an aligned legend
        spacing <- guides$width[5]
        guides <- gtable::gtable_add_cols(guides, hjust*spacing, 1)
        guides$widths[6] <- (1-hjust)*spacing
        title_index <- guides$layout$name == "title"
        guides$layout$l[title_index] <- 2
    
        # reconstruct guides and write back
        legend$grobs[[gi]] <- guides
      }
    
      # reconstruct legend and write back
      g$grobs[[legend_index]] <- legend
      g
    }
    

    The function is quite flexible and general. Here are a few examples of how it can be used:

    library(ggplot2)
    library(cowplot)
    #> 
    #> ********************************************************
    #> Note: As of version 1.0.0, cowplot does not change the
    #>   default ggplot2 theme anymore. To recover the previous
    #>   behavior, execute:
    #>   theme_set(theme_cowplot())
    #> ********************************************************
    library(colorspace)
    
    # single legend
    p <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Petal.Width)) + geom_point()
    ggdraw(align_legend(p)) # centered
    

    ggdraw(align_legend(p, hjust = 1)) # right aligned
    

    # multiple legends
    p2 <- ggplot(mtcars, aes(disp, mpg, fill = hp, shape = factor(cyl), size = wt)) + 
       geom_point(color = "white") +
       scale_shape_manual(values = c(23, 24, 21), name = "cylinders") +
       scale_fill_continuous_sequential(palette = "Emrld", name = "power (hp)", breaks = c(100, 200, 300)) +
       xlab("displacement (cu. in.)") +
       ylab("fuel efficiency (mpg)") +
       guides(
         shape = guide_legend(override.aes = list(size = 4, fill = "#329D84")),
         size = guide_legend(
           override.aes = list(shape = 21, fill = "#329D84"),
           title = "weight (1000 lbs)")
         ) +
       theme_half_open() + background_grid()
    
    # works but maybe not the expected result
    ggdraw(align_legend(p2))
    

    # more sensible layout
    ggdraw(align_legend(p2 + theme(legend.position = "top", legend.direction = "vertical")))
    

    Created on 2019-10-04 by the reprex package (v0.3.0)

    Original answer:

    I found a solution. It requires some digging into the grob tree, and it may not work if there are multiple legends, but otherwise this seems a reasonable solution until something better comes along.

    library(ggplot2)
    library(gtable)
    library(grid)
    
    p <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) + 
      geom_point(size = 3) +
      scale_color_distiller(palette = "YlGn", type = "seq", direction = -1,
                            name = "Long legend heading\nShould be centered") +
      theme(legend.title.align = 0.5)
    
    # extract legend
    g <- ggplotGrob(p)
    grobs <- g$grobs
    legend_index <- which(sapply(grobs, function(x) x$name) == "guide-box")
    legend <- grobs[[legend_index]]
    
    # extract guides table
    guides_index <- which(sapply(legend$grobs, function(x) x$name) == "layout")
    guides <- legend$grobs[[guides_index]]
    
    # add extra column for spacing
    # guides$width[5] is the extra spacing from the end of the legend text
    # to the end of the legend title. If we instead distribute it 50:50 on
    # both sides, we get a centered legend
    guides <- gtable_add_cols(guides, 0.5*guides$width[5], 1)
    guides$widths[6] <- guides$widths[2]
    title_index <- guides$layout$name == "title"
    guides$layout$l[title_index] <- 2
    
    # reconstruct legend and write back
    legend$grobs[[guides_index]] <- guides
    g$grobs[[legend_index]] <- legend
    
    grid.newpage()
    grid.draw(g)
    

提交回复
热议问题