R/geom-label-sci.R

#' @export
#' @rdname geom_text
#' @param label.padding Amount of padding around label. Defaults to 0.25 lines.
#' @param label.r Radius of rounded corners. Defaults to 0.15 lines.
#' @param label.size Size of label border, in mm.
geom_label_sciname <- function(mapping = NULL, data = NULL,
                       stat = "identity", position = "identity",
                       ...,
                       parse = TRUE,
                       nudge_x = 0,
                       nudge_y = 0,
                       label.padding = unit(0.25, "lines"),
                       label.r = unit(0.15, "lines"),
                       label.size = 0.25,
                       na.rm = FALSE,
                       show.legend = NA,
                       inherit.aes = TRUE) {
  if (!missing(nudge_x) || !missing(nudge_y)) {
    if (!missing(position)) {
      stop("Specify either `position` or `nudge_x`/`nudge_y`", call. = FALSE)
    }

    position <- position_nudge(nudge_x, nudge_y)
  }

  layer(
    data = data,
    mapping = mapping,
    stat = stat,
    geom = GeomLabelSciname,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      parse = parse,
      label.padding = label.padding,
      label.r = label.r,
      label.size = label.size,
      na.rm = na.rm,
      ...
    )
  )
}


#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
GeomLabelSciname <- ggproto("GeomLabelSciname", Geom,
                     required_aes = c("x", "y", "sci"),

                     default_aes = aes(
                       colour = "black", fill = "white", size = 3.88, angle = 0,
                       hjust = 0.5, vjust = 0.5, alpha = NA, family = "", fontface = 1,
                       lineheight = 1.2, sci = NA, nonsci = NULL, important=NULL
                     ),

                     draw_panel = function(self, data, panel_params, coord, parse = TRUE,
                                           na.rm = FALSE,
                                           label.padding = unit(0.25, "lines"),
                                           label.r = unit(0.15, "lines"),
                                           label.size = 0.25) {
                       # One if for each scenario


                       # All are present
                       if (!is.null(data$nonsci) & !is.null(data$important)){

                         sci <- ifelse(data$important,
                                       paste0("bolditalic('", data$sci, "')"),
                                       paste0("italic('", data$sci, "')"))

                         nonsci <- ifelse(data$important,
                                          paste0("bold('", data$nonsci, "')"),
                                          paste0("plain('", data$nonsci, "')"))


                         labe <- paste0(sci, "~", nonsci)


                       }

                       # Only Sci
                       if (is.null(data$nonsci) & is.null(data$important)){

                         sci <- paste0("italic('", data$sci, "')")

                         labe <- sci

                       }

                       # no nonsci, but important
                       if (is.null(data$nonsci) & !is.null(data$important)){

                         sci <- ifelse(data$important,
                                       paste0("bolditalic('", data$sci, "')"),
                                       paste0("italic('", data$sci, "')"))

                         labe <- sci
                       }

                       # nonsci, but no important

                       if (!is.null(data$nonsci) & is.null(data$important)){

                         sci <- paste0("italic('", data$sci, "')")
                         nonsci <-  paste0("plain('", data$nonsci, "')")
                         labe <- paste0(sci, "~", nonsci)

                       }

                       # sci- nonsci+ imp-
                       if (is.null(data$sci) & is.null(data$important)){
                         nonsci <- paste0("plain('", data$nonsci, "')")
                         labe <- nonsci

                       }

                       if (is.null(data$sci) & !is.null(data$important)){
                         nonsci <- ifelse(data$important,
                                          paste0("bold('", data$nonsci, "')"),
                                          paste0("plain('", data$nonsci, "')"))
                         labe <- nonsci
                       }

                       if (is.null(data$sci) & is.null(data$nonsci)){
                         stop('You must specify either sci or nonsci')
                       }
                       # END



                       if (parse) {
                         lab <- parse(text = as.character(labe))
                       }

                       data <- coord$transform(data, panel_params)
                       if (is.character(data$vjust)) {
                         data$vjust <- compute_just(data$vjust, data$y)
                       }
                       if (is.character(data$hjust)) {
                         data$hjust <- compute_just(data$hjust, data$x)
                       }

                       grobs <- lapply(1:nrow(data), function(i) {
                         row <- data[i, , drop = FALSE]
                         labelGrob(lab[i],
                                   x = unit(row$x, "native"),
                                   y = unit(row$y, "native"),
                                   just = c(row$hjust, row$vjust),
                                   padding = label.padding,
                                   r = label.r,
                                   text.gp = gpar(
                                     col = row$colour,
                                     fontsize = row$size * .pt,
                                     fontfamily = row$family,
                                     fontface = row$fontface,
                                     lineheight = row$lineheight
                                   ),
                                   rect.gp = gpar(
                                     col = row$colour,
                                     fill = alpha(row$fill, row$alpha),
                                     lwd = label.size * .pt
                                   )
                         )
                       })
                       class(grobs) <- "gList"

                       ggname("geom_label", grobTree(children = grobs))
                     },

                     draw_key = draw_key_label
)

ggname <- function(prefix, grob) {
  grob$name <- grobName(grob, prefix)
  grob
}


labelGrob <- function(label, x = unit(0.5, "npc"), y = unit(0.5, "npc"),
                      just = "center", padding = unit(0.25, "lines"), r = unit(0.1, "snpc"),
                      default.units = "npc", name = NULL,
                      text.gp = gpar(), rect.gp = gpar(fill = "white"), vp = NULL) {

  stopifnot(length(label) == 1)

  if (!is.unit(x))
    x <- unit(x, default.units)
  if (!is.unit(y))
    y <- unit(y, default.units)

  gTree(label = label, x = x, y = y, just = just, padding = padding, r = r,
        name = name, text.gp = text.gp, rect.gp = rect.gp, vp = vp, cl = "labelgrob")
}

#' @export
makeContent.labelgrob <- function(x) {
  hj <- resolveHJust(x$just, NULL)
  vj <- resolveVJust(x$just, NULL)

  t <- textGrob(
    x$label,
    x$x + 2 * (0.5 - hj) * x$padding,
    x$y + 2 * (0.5 - vj) * x$padding,
    just = c(hj, vj),
    gp = x$text.gp,
    name = "text"
  )

  r <- roundrectGrob(x$x, x$y, default.units = "native",
                     width = grobWidth(t) + 2 * x$padding,
                     height = grobHeight(t) + 2 * x$padding,
                     just = c(hj, vj),
                     r = x$r,
                     gp = x$rect.gp,
                     name = "box"
  )

  setChildren(x, gList(r, t))
}
Jtrachsel/ggscinames documentation built on May 13, 2019, 11:52 p.m.