将textGrob转换为imageGrob/rasterGrob? [英] convert textGrob to imageGrob/rasterGrob?

查看:185
本文介绍了将textGrob转换为imageGrob/rasterGrob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这很简单.其实我希望是这样!

Apologies if this is very straightforward. Actually I hope it will be!

我正在尝试从文本中动态创建图像,然后可以调整其大小并绘制(拉伸或压扁)以生成图案型图形.

I am trying to dynamically create images from text which I can then resize and plot (either stretched or squashed) to produce a motif-type graph.

我开始使用图像(由png()ggplot()生成),并将其绘制为annotation_custom()

I started out using images (which I'd generated using png() and ggplot()) and plotting them as annotation_custom()

require(ggplot2)
require(grid)
require(gridExtra)
qplot(c(0,10),c(0,10)) + 
annotation_custom(rasterGrob(image=readPNG("1999.png"),x=0,y=0,height=1,width=1,just=c("left","bottom")),
              xmin=0,xmax=5,ymin=0,ymax=7.5)

产生:

这很好,但是如果使用png()来动态创建大小不同的图像,则很麻烦,而且很难将其持久保存到文件中,因此我尝试查看是否可以使用textGrob:

This is fine, but it's awkward to create the images dynamically if they are not the same size, using png(), plus it's clunky to persist them to file, so I tried to see if I could use a textGrob:

myText<-"1000"
myTextGrob<-textGrob(myText,just=c("left","bottom"),gp=gpar(fontsize="100",col="red",fontfamily="Showcard Gothic"))
qplot(c(0,10),c(0,10))+annotation_custom(myTextGrob,0,0,0,0)

得到了,这很好,除了....

and got this, which is fine, except....

...似乎无法拉伸&以与rasterGrob相同的方式倾斜它,所以我的问题是-是否可以创建textGrob并将其强制转换为rasterGrob?还是有其他解决方案可以让我倾斜/拉伸textGrob?

...it doesn't seem possible to stretch & skew it in the same way as a rasterGrob so my question is - is it possible to create the textGrob and coerce it to a rasterGrob? Or is there another solution which will let me skew/stretch the textGrob?

提前谢谢!

推荐答案

不创建临时文件似乎并不容易.可以将矢量路径与grImport软件包一起使用,而不是栅格文件.有两种导入文本的选项,

It doesn't seem easy without creating temporary files. Instead of raster files, you might use vector paths with the grImport package. There are two options to import text,

  • 作为路径;它可以正常工作(下面的示例),但是没有明显的方法可以绕过中间文件将ps转换为xml的步骤

  • as a path; it works (example below), but there's no obvious way to bypass the ps to xml conversion step with intermediate files

作为文本字符串; xml在这种情况下要短得多,可以直接从R创建,但是很遗憾,我找不到独立地变换两个轴的方法.

as a text string; the xml is much shorter in this case, and could be created directly from R, but unfortunately I couldn't find a way to transform the two axes independently.

library(grImport)

scale_text <- function(text="hello world", scale=4, tmp=tempfile()){
  tmp.ps <- paste0(tmp, ".ps")
  tmp.xml <- paste0(tmp, ".xml")
  string.ps <- paste0('%!PS
                    /Courier             % name the desired font
                    20 selectfont        % choose the size in points and establish 
                    % the font as the current one
                    1 ', scale, ' scale            % scale axis
                    72 500 moveto        % position the current point at 
                    % coordinates 72, 500 (the origin is at the 
                    % lower-left corner of the page)
                    (', text, ') show  % stroke the text in parentheses
                    showpage             % print all on the page
                    ')
  cat(string.ps, file=tmp.ps)
  PostScriptTrace(tmp.ps, tmp.xml)
  readPicture(tmp.xml)
}


hello <- scale_text()
grid.newpage()
grid.picture(hello)

这篇关于将textGrob转换为imageGrob/rasterGrob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆