简体   繁体   中英

Remove all white margins around text from ggplot

Problem

I want to create a plot that contains only text and with as little white space around the text as possible. I tried using annotate() but there is still a lot of superfluous white space. Trying to adjust the axes limits via coord_cartesian() does not work, because the text just stays where it is and adjust to the new limits (see reprex).

The end goal is to only have a text box that I can then align with an image using patchwork using layout areas such that the lines formed by the text and the image align. Basically, I want to do the same thing with the flag and text as in my lastTidyTuesday contribution without having to move the texts and image bit by bit so that it eventually fits or rather is "close enough".

Reprex

library(ggplot2)
library(ggtext)
text <- paste("iris data set gives the measurements in cm",
             "of the variables sepal length and width",
             "and petal length and width, respectively,",
             "for 50 flowers from each of 3 species of iris.",
             "The species are Iris setosa, versicolor, and virginica.", sep = "\n")

ggplot() + 
  annotate(
    'text',
    x = 0,
    y = 1,
    label = text,
    hjust = 0,
    vjust = 1
  ) +
  coord_cartesian(xlim = c(0, 0.5), ylim = c(0, 1)) +
  theme(plot.margin = margin()) +
  theme_void()

ggplot() + 
  annotate(
    'text',
    x = 0,
    y = 1,
    label = text,
    hjust = 0,
    vjust = 1
  ) +
  coord_cartesian(xlim = c(0, 1), ylim = c(0, 1)) + ## changed axis limits here
  theme(plot.margin = margin()) +
  theme_void()

Created on 2022-03-10 by the reprex package (v2.0.0)

EDIT: Tried ggfittext as suggested but dit not work

library(ggplot2)
library(ggtext)
text <- paste('iris data set gives the measurements in cm',
             "of the variables sepal length and width",
             "and petal length and width, respectively,",
             "for 50 flowers from each of 3 species of iris.",
             'The species are Iris setosa, versicolor, and virginica.', sep = "\n")


library(ggfittext)
ggplot() +
  geom_fit_text(
    aes(label = text, x = 1, y = 1),
    hjust = 0, grow = T,
    padding.x = grid::unit(0, "mm"),
    padding.y = grid::unit(0, "mm")
  ) +
  coord_cartesian(xlim = c(0.55, 1.45), ylim = c(0.5, 1.5), expand = F) +
  theme(plot.margin = margin())


ggplot() +
  geom_fit_text(
    aes(label = text, x = 1, y = 1),
    hjust = 0, grow = T,
    padding.x = grid::unit(0, "mm"),
    padding.y = grid::unit(0, "mm")
  ) +
  # Adjusted the y-limits here
  coord_cartesian(xlim = c(0.55, 1.45), ylim = c(0.75, 1.25), expand = F) +
  theme(plot.margin = margin())

Created on 2022-03-10 by the reprex package (v2.0.0)

The ggfittext package is new to me but seems to do a nice job of this:

library(ggfittext)
ggplot() +
  geom_fit_text(
    aes(label = text, x = 1, y = 1),
    grow = TRUE, hjust = 0,
    padding.x = grid::unit(0, "mm"),
    padding.y = grid::unit(0, "mm")
  ) +
  theme_void() +
  theme(plot.margin = margin())

在此处输入图像描述

You will still need to resize the graphics window to the aspect ratio you want, but at least the text will scale with the limiting dimension of the window size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM