Browse Source

left/center/right alignment for custom legend

master
boB Rudis 5 years ago
parent
commit
db810d668c
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 53
      R/legend-helpers.R
  2. 9
      man/add_econodist_legend.Rd
  3. 6
      man/econodist_legend_grob.Rd

53
R/legend-helpers.R

@ -24,11 +24,14 @@ left_align <- function(gg, components){
#' @family Econodist legend helpers
#' @export
econodist_legend_grob <- function(family = "EconSansCndLig",
label_size = 10,
tenth_col = econ_tenth,
med_col = econ_median,
ninetieth_col = econ_ninetieth,
label_col = econ_text_col) {
label_size = 10,
tenth_lab = "10th percentile",
tenth_col = econ_tenth,
med_lab = "Median",
med_col = econ_median,
ninetieth_lab = "90th percentile",
ninetieth_col = econ_ninetieth,
label_col = econ_text_col) {
x_pos <- unit(4, "points")
y_pos <- unit(label_size / 2, "points")
@ -49,7 +52,7 @@ econodist_legend_grob <- function(family = "EconSansCndLig",
x_pos <- x_pos + convertUnit(grobWidth(tenth_seg), "points") + unit(6, "points")
textGrob(
label = "10th percentile",
label = tenth_lab,
x = x_pos, y = y_pos,
hjust = 0, vjust = 0.5,
gp = gpar(
@ -73,7 +76,7 @@ econodist_legend_grob <- function(family = "EconSansCndLig",
x_pos <- x_pos + convertUnit(grobWidth(med_pt), "points") + unit(8, "points")
textGrob(
label = "Median",
label = med_lab,
x = x_pos, y = y_pos,
hjust = 0, vjust = 0.5,
gp = gpar(
@ -99,7 +102,7 @@ econodist_legend_grob <- function(family = "EconSansCndLig",
x_pos <- x_pos + grobWidth(ninth_seg) + unit(8, "points")
textGrob(
label = "90th percentile",
label = ninetieth_lab,
x = x_pos, y = y_pos,
hjust = 0, vjust = 0.5,
gp = gpar(
@ -109,16 +112,19 @@ econodist_legend_grob <- function(family = "EconSansCndLig",
)
) -> ninth_text
vp <- viewport(default.units = "points")
width <- x_pos + grobWidth(ninth_text)
vp <- viewport(x = 0, just = "left", default.units = "points", width = width)
gTree(
name = "econodist_legend",
name = "econodist-legend",
children = gList(
tenth_seg, tenth_text,
med_pt, med_text,
ninth_seg, ninth_text
),
childrenvp = vp
childrenvp = vp,
vp = viewport(x = 0, just = "left", width = width)
)
}
@ -128,23 +134,46 @@ econodist_legend_grob <- function(family = "EconSansCndLig",
#' @param gg ggplot2 plot object to add
#' @param legend legend grob (any grob, really)
#' @param below which named gtable element to stick it below?
#' @param just legend position: "`left`" (aligned with left veritcal axis),
#' "`center`" (center of plot panel), or "`right`" (aligned with right
#' vertical axis). Uses [pmatch()] so partial matching is supported.
#' @param legend_height height of the legend row
#' @param spacer height of the spacer that is put below `legend`?
#' @family Econodist legend helpers
#' @export
add_econodist_legend <- function(gg, legend, below = "subtitle",
just = c("left", "center", "right"),
legend_height = unit(16, "points"),
spacer = unit(10, "points")) {
choices <- c("left", "center", "right")
just <- choices[pmatch(just[1], choices, duplicates.ok = FALSE)]
if (!inherits(gg, "gtable")) gg <- ggplot2::ggplotGrob(gg)
st <- gg$layout[gg$layout$name == below,]
gtable::gtable_add_rows(
gtable::gtable_add_grob(
gtable::gtable_add_rows(gg, legend_height, st$b),
legend, t = st$b + 1, l = st$l, b = st$b + 1, r = st$r
legend, t = st$b + 1, l = st$l, b = st$b + 1, r = st$r,
name = "econodist-legend"
),
spacer, st$b + 1
) -> gg2
w <- gg2$grobs[[which(gg2$layout$name == "econodist-legend")]]$vp$width
hw <- unit(as.numeric(convertUnit(w, "points"))/2, "points")
switch(
just,
left = gg2$grobs[[which(gg2$layout$name == "econodist-legend")]]$vp$x <-
unit(0, "npc"),
right = gg2$grobs[[which(gg2$layout$name == "econodist-legend")]]$vp$x <-
unit(1, "npc") - w,
center = gg2$grobs[[which(gg2$layout$name == "econodist-legend")]]$vp$x <-
unit(0.5, "npc") - hw
)
gg2
}

9
man/add_econodist_legend.Rd

@ -4,8 +4,9 @@
\alias{add_econodist_legend}
\title{Helper utility to get an econodist legend into a ggplot2 plot}
\usage{
add_econodist_legend(gg, legend, below = "subtitle",
legend_height = unit(16, "points"), spacer = unit(10, "points"))
add_econodist_legend(gg, legend, below = "subtitle", just = c("left",
"center", "right"), legend_height = unit(16, "points"),
spacer = unit(10, "points"))
}
\arguments{
\item{gg}{ggplot2 plot object to add}
@ -14,6 +15,10 @@ add_econodist_legend(gg, legend, below = "subtitle",
\item{below}{which named gtable element to stick it below?}
\item{just}{legend position: "\code{left}" (aligned with left veritcal axis),
"\code{center}" (center of plot panel), or "\code{right}" (aligned with right
vertical axis). Uses \code{\link[=pmatch]{pmatch()}} so partial matching is supported.}
\item{legend_height}{height of the legend row}
\item{spacer}{height of the spacer that is put below \code{legend}?}

6
man/econodist_legend_grob.Rd

@ -5,8 +5,10 @@
\title{Create a legend grob that can be used with econodist charts}
\usage{
econodist_legend_grob(family = "EconSansCndLig", label_size = 10,
tenth_col = econ_tenth, med_col = econ_median,
ninetieth_col = econ_ninetieth, label_col = econ_text_col)
tenth_lab = "10th percentile", tenth_col = econ_tenth,
med_lab = "Median", med_col = econ_median,
ninetieth_lab = "90th percentile", ninetieth_col = econ_ninetieth,
label_col = econ_text_col)
}
\arguments{
\item{family}{font family}

Loading…
Cancel
Save