最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - How to plot text only, without any margin, in a tiff file? - Stack Overflow

programmeradmin1浏览0评论

I want to build a tiff file with text only and no margins around the text, using R.

I tried :

library(ggplot2)

plt <- ggplot() +
  geom_text(aes(x=0,y=0,label="test")) +
  theme_void()

ggsave(plt,filename="test.tif",bg="transparent")

However, there are large margins (I have added the borderline to highlight these margins):

What I want to get is a file with the image of the text, without any margin:

Is there a way to create such a tiff file, using ggplot, plot or any other R function?

I want to build a tiff file with text only and no margins around the text, using R.

I tried :

library(ggplot2)

plt <- ggplot() +
  geom_text(aes(x=0,y=0,label="test")) +
  theme_void()

ggsave(plt,filename="test.tif",bg="transparent")

However, there are large margins (I have added the borderline to highlight these margins):

What I want to get is a file with the image of the text, without any margin:

Is there a way to create such a tiff file, using ggplot, plot or any other R function?

Share Improve this question asked Mar 22 at 17:16 Jean-Luc DupoueyJean-Luc Dupouey 4813 silver badges11 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 7

This is pretty close, in base R: thanks to @r2evans for code to auto-adjust the width/height of the image:

txt <- "hello world"
tiff("test.tiff", strwidth(txt, units="in"), 
       strheight(txt, units="in"), units="in", res=300)
par(mar = rep(0,4))
plot(0:1, 0:1, type = "n", ann = FALSE, axes=FALSE)
text(0.5, 0.5, txt)
dev.off()

You can use the library magick with an empty canvas - annotate text and trim of the margins and it will give

I tried methods described here and here but they used R's devices like tiff but the strwidth / strheight methods were very unsatisfactory, also aligning the text along a grid or even ggplot2 turned out to still leave unwanted margins or even cut off text.

words <- c("The", "Turing", "test,", "originally", "called", "the", "imitation", 
           "game", "by", "Alan", "Turing", "in", "1949,[2]", "is", "a", 
           "test", "of", "a", "machine's", "ability", "a", "b", "c", "d", 
           "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", 
           "r", "s", "t", "u", "v", "w", "x", "y", "z")

library(magick)

text2tiff <- function(
    text,
    font_size = 500,
    font_family = "Arial",
    backgroundColor = "white",
    textColor = "black")
{

  width <- ceiling(strwidth(text, units = "inches", family = font_family,ps = par(ps = font_size)) * 96 + 200)
  height <- ceiling(strheight(text, units = "inches", family = font_family, ps = par(ps = font_size)) * 96 + 200)
  
  canvas <- image_blank(width = width, height = height, color = backgroundColor)
  
  text_image <- image_annotate(canvas, # Annotate text
                               text, 
                               font = font_family,
                               size = font_size,
                               color = textColor,
                               gravity = "center")

  image_write(image_trim(text_image), # Trim image to remove margins
              path = paste0(gsub("[^[:alnum:]]", "", text), ".tiff"), # take text as img name
              format = "tiff")
}

for(i in 1:length(words)) text2tiff(words[i])
  • For increased image quality, increase the fontsize
  • Adjust the file naming, don't know how long your text / word snippets are, they might exceed your OS' maximum file name

Update: Thanks for your comment: According to the answer of @Tim G we could us ggplot and magick :

library(ggplot2)
library(magick)

plt <- ggplot() +
  geom_text(aes(x = 0, y = 0, label = "hello world"), size = 10) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_void() +
  theme(plot.margin = margin(0, 0, 0, 0, "pt"))

# temporary file
temp_file <- tempfile(fileext = ".tif")
ggsave(temp_file, plot = plt, bg = "white",
       width = 4, height = 2, units = "in", dpi = 300)


img <- image_read(temp_file)
img_cropped <- image_trim(img) # Crop the image to remove whitespace

image_write(img_cropped, "test_cropped.tif") # Save the cropped image

------------------------------------------

First answer:

With `ggplot2` we could remove extra space by setting `expand = c(0,0)` and remove some padding by setting `plot.margin()`:

library(ggplot2)
library(grid)  

plt <- ggplot() +
  geom_text(aes(x = 0, y = 0, label = "hello world"), size = 10) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_void() +
  theme(plot.margin = margin(0, 0, 0, 0, "pt"))

ggsave("test.tif", plot = plt, bg = "white",
       width = 2, height = 1, units = "in", dpi = 300)
发布评论

评论列表(0)

  1. 暂无评论