1

I am struggling to modify ggplot2 annotate(). All I want to do, is create a slightly modified annotate() function (say, annotate_new()) which automatically places annotations in the top left corner with a certain text size.

Below, please find my try which somehow doesn't work:

annotate_new <- 
  function (geom, xmin = NULL, xmax = NULL, 
            ymin = NULL, ymax = NULL, xend = NULL, yend = NULL, 
            x=NULL, y=NULL, parse=TRUE, size = 12, ..., 
            na.rm = FALSE) {
    position <- compact(list(xmin = xmin, xmax = xmax, 
                             xend = xend, ymin = ymin, ymax = ymax, yend = yend,
                             x = -Inf, y = Inf, 
                             hjust = 0, vjust = 1))
    aesthetics <- c(position, list(...))
    lengths <- vapply(aesthetics, length, integer(1))
    n <- unique(lengths)
    if (length(n) > 1L) {
      n <- setdiff(n, 1L)
    }
    if (length(n) > 1L) {
      bad <- lengths != 1L
      details <- paste(names(aesthetics)[bad], " (", 
                       lengths[bad], ")", sep = "", collapse = ", ")
      stop("Unequal parameter lengths: ", details, call. = FALSE)
    }
    data <- vctrs::new_data_frame(position, n = n)
    layer(geom = "text", 
          params = list(na.rm = na.rm, ...), stat = StatIdentity, 
          position = PositionIdentity, data = data, mapping = aes_all(names(data)), 
          inherit.aes = FALSE, show.legend = FALSE)
  }


ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  annotate_new(label = corr_eqn(mpg$hwy, mpg$displ))

This creates:

enter image description here

Somehow, the text size isn't adjustable... Would really appreciate your help!

2
  • Rather than trying to create a new annotation style, why don't you put your formatted figure in its own function so you call your_plot_function(x,y,labelx, labely, annotation) and it produces your figure? Commented May 19, 2020 at 12:35
  • because I would use this function over and over again (for several hundred plots) Commented May 19, 2020 at 12:41

2 Answers 2

2

In your code, call size in the list of parmeters

annotate_new <- 
  function (geom, xmin = NULL, xmax = NULL, 
            ymin = NULL, ymax = NULL, xend = NULL, yend = NULL, 
            x=NULL, y=NULL, parse=TRUE, size = 12, ..., 
            na.rm = FALSE) {
    position <- compact(list(xmin = xmin, xmax = xmax, 
                             xend = xend, ymin = ymin, ymax = ymax, yend = yend,
                             x = -Inf, y = Inf, 
                             hjust = 0, vjust = 1))
    aesthetics <- c(position, list(...))
    lengths <- vapply(aesthetics, length, integer(1))
    n <- unique(lengths)
    if (length(n) > 1L) {
      n <- setdiff(n, 1L)
    }
    if (length(n) > 1L) {
      bad <- lengths != 1L
      details <- paste(names(aesthetics)[bad], " (", 
                       lengths[bad], ")", sep = "", collapse = ", ")
      stop("Unequal parameter lengths: ", details, call. = FALSE)
    }
    data <- vctrs::new_data_frame(position, n = n)
    layer(geom = "text", 
          params = list(na.rm = na.rm, size = size, ...), stat = StatIdentity, 
          position = PositionIdentity, data = data, mapping = aes_all(names(data)), 
          inherit.aes = FALSE, show.legend = FALSE)
  }


ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  annotate_new(label = "temp", size = 13)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of re-writing the source code would you be open to creating a wrapper for the annotate function?

annotate_new <- function(...){
  annotate('text',
           x = -Inf,
           y = Inf,
           size = 12, 
           hjust = 0,
           vjust = 1,
           ...)
}

ggplot(mpg, aes(displ, hwy)) + 
  geom_point()+
  annotate_new(label="Label")

This is the same function with some of your preferred defaults set and you can still pass through additional arguments via the '...' like the 'label'.

enter image description here

1 Comment

While the answer above is the correct one to my question, I love yours and will use it. I somewhat stupidly didn't think about creating a wrapper on the function. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.