knitr::opts_chunk$set(echo = TRUE)

Resizing text tests

library(metacoder)
text_data <- data.frame(stringsAsFactors = FALSE, 
                        label = c("1", "Short", "Kinda_average", "Quite a lengthy label", "Way toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo long", "S  p   a   c   e   y", "new \n best ever, most awesomnest \n line"), 
                        x = c(1, 2, 4, 8, 16, 32, 64),
                        y = c(1, 2, 4, 8, 16, 32, 64),
                        size = c(1, 2, 4, 8, 16, 32, 26) * 0.1,
                        color = "black",
                        rotation = c(1, 2, 3, 4, 5, 6, 7) * pi * 0.2,
                        justification = c("center", "left-center", "right-top", "left-bottom", "center-top", "center-bottom", "right-center"))
label_corners <- metacoder:::label_bounds(label = text_data$label, x = text_data$x, y = text_data$y,
                                          height = text_data$size, rotation = text_data$rotation,
                                          just = text_data$justification)
x_range <- range(label_corners$x)
y_range <- range(label_corners$y)
library(ggplot2)
the_plot <- ggplot(data = data) +
  geom_polygon(data = label_corners, aes_string(x = "x", y = "y", group = "label"), fill = "transparent", color = "black")+
  ggplot2::coord_fixed(xlim = x_range, ylim = y_range) +
  ggplot2::scale_y_continuous(expand = c(0,0), limits = y_range) +
  ggplot2::scale_x_continuous(expand = c(0,0), limits = x_range) +
  theme(panel.grid = element_blank(),
        axis.title = element_blank(),
        axis.text  = element_blank(),
        axis.ticks = element_blank(), 
        axis.line  = element_blank(),
        plot.margin = grid::unit(c(0,0,0,0) , "in"))
text_grobs <- do.call(metacoder:::make_text_grobs, c(text_data, list(x_range = x_range, y_range = y_range)))
for (a_grob in text_grobs) {
  the_plot <- the_plot + annotation_custom(grob = a_grob)
}
the_plot


grunwaldlab/metacoder documentation built on Feb. 22, 2024, 3:47 a.m.