GithubHelp home page GithubHelp logo

Comments (9)

clauswilke avatar clauswilke commented on July 30, 2024

If I understand correctly, this would add one single label to the entire figure, not each graph in a plot grid, correct? In this case, it's functionality that doesn't belong into plot_grid(), since one might want such a label also on an individual plot. I'll consider adding a convenience function that does this.

from cowplot.

ustervbo avatar ustervbo commented on July 30, 2024

Yes - it would add a single label to the entire figure.

A convenience function would also work

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024

I don't mind adding a convenience function. If you can draft one or prepare a pull request that will increase the likelihood of me adding this quickly.

from cowplot.

ustervbo avatar ustervbo commented on July 30, 2024

Will this work:

draw_grid_label <- function(label, position = c("top.left", "top", "top.right", "bottom.left", "bottom", "bottom.right"), size, fontface, ...){
  # Get the position
  position <- match.arg(position)

  # Set default font size and face from the theme
  if(missing(size)){
    size <- theme_get()$text$size  
  }
  if(missing(fontface)){
    fontface <- theme_get()$text$face  
  }
  switch(position,
         top.left     = draw_plot_label(label, x = 0,   y = 1, hjust = -0.1, vjust = 1.1,  size = size, fontface = fontface, ...),
         top          = draw_plot_label(label, x = 0.5, y = 1, hjust = 0,    vjust = 1.1,  size = size, fontface = fontface, ...),
         top.right    = draw_plot_label(label, x = 1,   y = 1, hjust = 1.1,  vjust = 1.1,  size = size, fontface = fontface, ...),
         bottom.left  = draw_plot_label(label, x = 0,   y = 0, hjust = -0.1, vjust = -0.1, size = size, fontface = fontface, ...),
         bottom       = draw_plot_label(label, x = 0.5, y = 0, hjust = 0,    vjust = -0.1, size = size, fontface = fontface, ...),
         bottom.right = draw_plot_label(label, x = 1,   y = 0, hjust = 1.1,  vjust = -0.1, size = size, fontface = fontface, ...)
  )
}

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024

I think so. Could you write the required documentation as well, preferably including some examples? Thanks!

from cowplot.

ustervbo avatar ustervbo commented on July 30, 2024

A little late, but here it is:

#' Add a label to a grid
#' 
#' @description 
#' Add a label to a grid
#'
#' @param label Label to be drawn
#' @param position Position of the label, can be one of "top.left", "top", "top.right", "bottom.left", "bottom", "bottom.right". Default is "top.left"
#' @param size (optional) Size of the label to be drawn. Default is the text size of the current theme
#' @param fontface (optional) Font face of the label to be drawn. Default is the font face of the current theme
#' @param ... other arguments passed to \code{draw_plot_label}
#'
#' @examples
#' 
#' p1 <- qplot(1:10, 1:10)
#' p2 <- qplot(1:10, (1:10)^2)
#' p3 <- qplot(1:10, (1:10)^3)
#' p4 <- qplot(1:10, (1:10)^4)
#' 
#' # Create a simple grid
#' p <- plot_grid(p1, p2, p3, p4)
#' 
#' # Default font size and position
#' p + draw_grid_label(label = "Figure 1")
#' 
#' # Different position and font size
#' p + draw_grid_label(label = "Figure 1", position = "bottom.right", size = 10)
#' 
#' # Using bold font face
#' p + draw_grid_label(label = "Figure 1", fontface = "bold")
#' 
#' # Making the label red and slanted
#' p + draw_grid_label(label = "Figure 1", angle = -45, colour = "red")
#' 
#' @author Ulrik Stervbo (ulrik.stervbo [AT] gmail.com)
#' 
draw_grid_label <- function(label, position = c("top.left", "top", "top.right", "bottom.left", "bottom", "bottom.right"), size, fontface, ...){
  # Get the position
  position <- match.arg(position)

  # Set default font size and face from the theme
  if(missing(size)){
    size <- theme_get()$text$size  
  }
  if(missing(fontface)){
    fontface <- theme_get()$text$face  
  }

  # Call draw_plot_label() with appropriate label positions
  switch(position,
         top.left     = draw_plot_label(label, x = 0,   y = 1, hjust = -0.1, vjust = 1.1,  size = size, fontface = fontface, ...),
         top          = draw_plot_label(label, x = 0.5, y = 1, hjust = 0,    vjust = 1.1,  size = size, fontface = fontface, ...),
         top.right    = draw_plot_label(label, x = 1,   y = 1, hjust = 1.1,  vjust = 1.1,  size = size, fontface = fontface, ...),
         bottom.left  = draw_plot_label(label, x = 0,   y = 0, hjust = -0.1, vjust = -0.1, size = size, fontface = fontface, ...),
         bottom       = draw_plot_label(label, x = 0.5, y = 0, hjust = 0,    vjust = -0.1, size = size, fontface = fontface, ...),
         bottom.right = draw_plot_label(label, x = 1,   y = 0, hjust = 1.1,  vjust = -0.1, size = size, fontface = fontface, ...)
  )
}

Let me know if there are any problems

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024

Thanks. I'm extremely busy right now, but I've added this to the TODO list and I'll get to it eventually.

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024

This is now on github and will be part of the next release. Note that I changed the name of the function to draw_figure_label since the function is not specific to plot grids and works on individual figures as well.

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024

This is on CRAN now.

from cowplot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.