Comments (9)
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.
Yes - it would add a single label to the entire figure.
A convenience function would also work
from cowplot.
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.
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.
I think so. Could you write the required documentation as well, preferably including some examples? Thanks!
from cowplot.
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.
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.
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.
This is on CRAN now.
from cowplot.
Related Issues (20)
- Conversion failure warnings on unicode character HOT 3
- Label positioning is inconsistent HOT 1
- Rstudio crashed on iOS saving plots into tiff format HOT 6
- [Feature request] Icicle plot with ggplots objects
- Using expressions in plot text generates warnings
- Feature request: Add "units" parameter to all functions where size can be specified.
- Odd problem with setting alpha in an annotate geom
- [Feature request] Align legend with the start and end of the axis
- Cannot convert object of class LayerInstanceLayerggprotogg into a grob?
- changing size of the second plot in plot_grid() HOT 2
- ggarange common.legend positioning overwritten
- No side effect from plot_grid when used inside a function returning an object
- get_legend doesn't match original position
- Blank plot using plot_grid with RNotebook
- Feature request: options for anchors of labels in plot_grid HOT 1
- warning message: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0. HOT 5
- Explanation of how align_plot, ggdraw, and draw_plot work together for dual-y-axis plots.
- `draw_image` only works when background themes are set to `ggplot2::element_blank()` HOT 1
- Aligning legends?
- plot_grid not working HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cowplot.