R/examples.R

#' @export
#' @import ggplot2
gglabeleditor_demo <- function() {
  cars <- mtcars
  cars$model <- row.names(cars)
  p <- ggplot(cars, aes(x = wt, y = disp)) +
    geom_point()
  extra_aes = aes(label = model, key = model)
  out <- gglabeleditor(p, 800, 400, extra_aes, edits_file = "labels.rds", label.padding = unit(0.1, "lines"), vjust=0, hjust =0)

  #p + geom_label_editable(extra_aes, edits = out$edits, label.padding = unit(0.1, "lines"), vjust=0, hjust =0 )
  #p + do.call(geom_text, c(list(mapping = aes(x = x, y = y, label = label), data = out$final_data, inherit.aes=F)), as.list(out$final_params)))
  #p + geom_text(data = out$final, mapping = aes(x = x, y = y, label = label))

  ### TODO: copy code like this to clipboard when DONE clicked

  png("example.png", width=800, height=400)
  # ------------------------------------------------------------------------------
  # This code is generated by gglabeleditor. To edit interactively, run
  # > eval(gglabeleditor_output$call)
  gglabeleditor_output <- readRDS("labels.rds")
  lp <- p +
    do.call(geom_text,
            c(list(mapping = aes(x = x, y = y, label = label),
                   data = gglabeleditor_output$final_data, inherit.aes=F),
              as.list(gglabeleditor_output$final_params)))
  # ------------------------------------------------------------------------------
  lp
  dev.off()
}
econandrew/gglabeleditor documentation built on May 3, 2019, 2:57 p.m.