-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add tag label for adding identifier to plot #2405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
92e955a
7421718
f1f8e1e
c08bad2
c937a19
5321b6a
19d8c84
b385981
12646f8
4ebe9e1
4fb2b23
31f9831
dfa0bc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -242,6 +242,11 @@ ggplot_gtable.ggplot_built <- function(data) { | |
subtitle <- element_render(theme, "plot.subtitle", plot$labels$subtitle, margin_y = TRUE) | ||
subtitle_height <- grobHeight(subtitle) | ||
|
||
# Tag | ||
tag <- element_render(theme, "plot.tag", plot$labels$tag, margin_y = TRUE, margin_x = TRUE) | ||
tag_height <- grobHeight(tag) | ||
tag_width <- grobWidth(tag) | ||
|
||
# whole plot annotation | ||
caption <- element_render(theme, "plot.caption", plot$labels$caption, margin_y = TRUE) | ||
caption_height <- grobHeight(caption) | ||
|
@@ -261,6 +266,75 @@ ggplot_gtable.ggplot_built <- function(data) { | |
plot_table <- gtable_add_grob(plot_table, caption, name = "caption", | ||
t = -1, b = -1, l = min(pans$l), r = max(pans$r), clip = "off") | ||
|
||
plot_table <- gtable_add_rows(plot_table, unit(0, 'pt'), pos = 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need all these new rows and cols? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This goes back to the discussion of whether plot tags should live in their own table cells or not. I've come around to @thomasp85 position that there may be valid uses for it, in particular when people try to (ab)use plot tags for things we're not envisioning right now. The moment people use entire words or phrases as plot tags, having them in separate cells will be useful. And I know from writing my book that that's something I have done with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should they always be added even when there isn't a tag? I don't have sense of if adding new rows/cols will disrupt existing code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea here, which is formed both by my work with facets and my extensions, is that it is much nicer to have a predictable gtable layout to work with. This is also why rows and columns are added for axes and strips on all sides even if they aren’t drawn. If any external code depend on the gtable layout it will break either way when users start to add tags, but doing it this way makes it much easier to fix as you don’t need any gtable introspection to see if a tag is added, and where. There is nothing inside ggplot2 that breaks so this is more a breaking change for extensions |
||
plot_table <- gtable_add_cols(plot_table, unit(0, 'pt'), pos = 0) | ||
plot_table <- gtable_add_rows(plot_table, unit(0, 'pt'), pos = -1) | ||
plot_table <- gtable_add_cols(plot_table, unit(0, 'pt'), pos = -1) | ||
|
||
tag_pos <- theme$plot.tag.position | ||
if (length(tag_pos) == 2) tag_pos <- "manual" | ||
valid_pos <- c("topleft", "top", "topright", "left", "right", "bottomleft", | ||
"bottom", "bottomright") | ||
if (!(tag_pos == "manual" || tag_pos %in% valid_pos)) { | ||
stop("plot.tag.position should be a coordinate or one of ", | ||
paste(valid_pos, collapse = ', '), call. = FALSE) | ||
} | ||
|
||
if (tag_pos == "manual") { | ||
xpos <- theme$plot.tag.position[1] | ||
ypos <- theme$plot.tag.position[2] | ||
tag_parent <- justify_grobs(tag, x = xpos, y = ypos, | ||
hjust = theme$plot.tag$hjust, | ||
vjust = theme$plot.tag$vjust, | ||
debug = theme$plot.tag$debug) | ||
plot_table <- gtable_add_grob(plot_table, tag_parent, name = "tag", t = 1, | ||
b = nrow(plot_table), l = 1, | ||
r = ncol(plot_table), clip = "off") | ||
} else { | ||
# Widths and heights are reassembled below instead of assigning into them | ||
# in order to avoid bug in grid 3.2 and below. | ||
if (tag_pos == "topleft") { | ||
plot_table$widths <- unit.c(tag_width, plot_table$widths[-1]) | ||
plot_table$heights <- unit.c(tag_height, plot_table$heights[-1]) | ||
plot_table <- gtable_add_grob(plot_table, tag, name = "tag", | ||
t = 1, l = 1, clip = "off") | ||
} else if (tag_pos == "top") { | ||
plot_table$heights <- unit.c(tag_height, plot_table$heights[-1]) | ||
plot_table <- gtable_add_grob(plot_table, tag, name = "tag", | ||
t = 1, l = 1, r = ncol(plot_table), | ||
clip = "off") | ||
} else if (tag_pos == "topright") { | ||
plot_table$widths <- unit.c(plot_table$widths[-ncol(plot_table)], tag_width) | ||
plot_table$heights <- unit.c(tag_height, plot_table$heights[-1]) | ||
plot_table <- gtable_add_grob(plot_table, tag, name = "tag", | ||
t = 1, l = ncol(plot_table), clip = "off") | ||
} else if (tag_pos == "left") { | ||
plot_table$widths <- unit.c(tag_width, plot_table$widths[-1]) | ||
plot_table <- gtable_add_grob(plot_table, tag, name = "tag", | ||
t = 1, b = nrow(plot_table), l = 1, | ||
clip = "off") | ||
} else if (tag_pos == "right") { | ||
plot_table$widths <- unit.c(plot_table$widths[-ncol(plot_table)], tag_width) | ||
plot_table <- gtable_add_grob(plot_table, tag, name = "tag", | ||
t = 1, b = nrow(plot_table), l = ncol(plot_table), | ||
clip = "off") | ||
} else if (tag_pos == "bottomleft") { | ||
plot_table$widths <- unit.c(tag_width, plot_table$widths[-1]) | ||
plot_table$heights <- unit.c(plot_table$heights[-nrow(plot_table)], tag_height) | ||
plot_table <- gtable_add_grob(plot_table, tag, name = "tag", | ||
t = nrow(plot_table), l = 1, clip = "off") | ||
} else if (tag_pos == "bottom") { | ||
plot_table$heights <- unit.c(plot_table$heights[-nrow(plot_table)], tag_height) | ||
plot_table <- gtable_add_grob(plot_table, tag, name = "tag", | ||
t = nrow(plot_table), l = 1, r = ncol(plot_table), clip = "off") | ||
} else if (tag_pos == "bottomright") { | ||
plot_table$widths <- unit.c(plot_table$widths[-ncol(plot_table)], tag_width) | ||
plot_table$heights <- unit.c(plot_table$heights[-nrow(plot_table)], tag_height) | ||
plot_table <- gtable_add_grob(plot_table, tag, name = "tag", | ||
t = nrow(plot_table), l = ncol(plot_table), clip = "off") | ||
} | ||
} | ||
|
||
# Margins | ||
plot_table <- gtable_add_rows(plot_table, theme$plot.margin[1], pos = 0) | ||
plot_table <- gtable_add_cols(plot_table, theme$plot.margin[2]) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need a brief explanation of what you might use a tag for here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 5f70fd8