Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix legend justification inside plot

  • Loading branch information...
commit 224b7b80db0d832885c6e1db74b792fbbd56ee27 1 parent b805680
@wch wch authored
Showing with 24 additions and 11 deletions.
  1. +10 −2 R/guides-.r
  2. +14 −9 R/plot-render.r
View
12 R/guides-.r
@@ -232,12 +232,19 @@ guides_build <- function(ggrobs, theme) {
widths <- do.call("unit.c", lapply(ggrobs, function(g)sum(g$widths)))
heights <- do.call("unit.c", lapply(ggrobs, function(g)sum(g$heights)))
+ # Set the justification of each legend within the legend box
+ # First value is xjust, second value is yjust
+ just <- valid.just(theme$legend.box.just)
+ xjust <- just[1]
+ yjust <- just[2]
+
# setting that is different for vergical and horizontal guide-boxes.
if (theme$legend.box == "horizontal") {
# Set justification for each legend
for (i in seq_along(ggrobs)) {
ggrobs[[i]] <- editGrob(ggrobs[[i]],
- vp = viewport(y = 1, just = "top", height = heightDetails(ggrobs[[i]])))
+ vp = viewport(x = xjust, y = yjust, just = c(xjust, yjust),
+ height = heightDetails(ggrobs[[i]])))
}
guides <- gtable_row(name = "guides",
@@ -251,7 +258,8 @@ guides_build <- function(ggrobs, theme) {
# Set justification for each legend
for (i in seq_along(ggrobs)) {
ggrobs[[i]] <- editGrob(ggrobs[[i]],
- vp = viewport(x = 0, just = "left", width = widthDetails(ggrobs[[i]])))
+ vp = viewport(x = xjust, y = yjust, just = c(xjust, yjust),
+ width = widthDetails(ggrobs[[i]])))
}
guides <- gtable_col(name = "guides",
View
23 R/plot-render.r
@@ -86,20 +86,25 @@ ggplot_gtable <- function(data) {
# these are a bad hack, since it modifies the contents fo viewpoint directly...
legend_width <- legend_width + theme$legend.margin
legend_height <- legend_height + theme$legend.margin
- # vp size = grob size. This enables justification in gtable.
- legend_box$childrenvp$parent$width <- legend_width
- legend_box$childrenvp$parent$height <- legend_height
- legend_box$childrenvp$parent$justification <- theme$legend.justification %||% "center"
- legend_box$childrenvp$parent$valid.just <- valid.just(theme$legend.justification)
+
+ # Set the justification of the legend box
+ # First value is xjust, second value is yjust
+ just <- valid.just(theme$legend.justification)
+ xjust <- just[1]
+ yjust <- just[2]
if (position == "manual") {
+ xpos <- theme$legend.position[1]
+ ypos <- theme$legend.position[2]
+
# x and y are specified via theme$legend.position (i.e., coords)
- legend_box$childrenvp$parent$x <- unit(coords[1], "npc")
- legend_box$childrenvp$parent$y <- unit(coords[2], "npc")
+ legend_box <- editGrob(legend_box,
+ vp = viewport(x = xpos, y = ypos, just = c(xjust, yjust),
+ height = gtable_height(legend_box), width = gtable_width(legend_box)))
} else {
# x and y are adjusted using justification of legend box (i.e., theme$legend.justification)
- legend_box$childrenvp$parent$x <- unit(legend_box$childrenvp$parent$valid.just[1], "npc")
- legend_box$childrenvp$parent$y <- unit(legend_box$childrenvp$parent$valid.just[2], "npc")
+ legend_box <- editGrob(legend_box,
+ vp = viewport(x = xjust, y = yjust, just = c(xjust, yjust)))
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.