538-ify ggplot输出:在R网格图形中验证pictureGrob [英] 538-ify ggplot output: justifying pictureGrob in R grid graphics

查看:182
本文介绍了538-ify ggplot输出:在R网格图形中验证pictureGrob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,但这似乎取决于一个 stringWidth 实用程序函数,它没有图片的合作伙伴(?)]



我已经提出了一个类似的问题,关于 gridextra表格,但作为baptiste的评论,'理由总是相当尴尬,以处理网格'。



任何帮助表示赞赏;可以重复使用的代码(注意 grImport c>需要ghostscript安装):

  require(grImport)
require(ggplot2)
require gridExtra)

#nb:你还需要在你的机器上安装ghostscript。
#我有一段时间让ghostscript正常工作。
#从R邮件列表的肠子里,我发现这个有用的提示
#from Paul Murrell本人:
#https://www.mail-archive.com/r-help@ r-project.org/msg203180.html
$ b $##YMMV,这个工作在我的机器上:
#gswin< - shortPathName(C:/ Program Files / gs / gs9.15 / bin / gswin64c.exe)
#Sys.setenv(R_GSCMD = gswin)


#有趣的小月亮
PostScriptTrace(
file = file.path(system.file(package =RGraphics),extra,comic_moon.ps),
outfilename =comic_moon.xml)
moon < - readPicture(comic_moon

$ b annotate_me< - 函数b
$ b #make a gTree
foo< - gTree(
children = gList(
rectGrob(gp = gpar(fill = bg_c​​olor)),
pictureGrob (
picture = some_picture,
x = 0,y = 0.5,
#width =?




final< - arrangeGrob(
plo t_to_annotate,foo,
nrow = 2,heights = c(19,1)


return(final)
}


annotate_me(
plot_to_annotate = qplot(mpg,data = mtcars),
some_picture = moon


解决方案

我相信widthDetails没有为pictureGrobs实现,所以说理由是随机的。

以下是一种方法:定义对象的宽度,并将其放置在x = 0.5宽度处。 $ b

  require(grImport)
(Picture,
paths = list(new(PictureFill,
x = c(0,1,0,1)),
y = c(0,0 ,1,1))),
summary = new(PictureSummary,
numPaths = 1,
xscale = c(0,1),
图片Grob(沙漏,use.gc = FALSE,x =单位(1,行),
宽度=单位(2,line),gp = gpar(fill =red))
library(ggplot2)
library(gtable)
library(dplyr)

p <-qplot(1,1)
g < - ggplotGrob(p)

grid.newpage()
g%>%
gtable_add_rows(unit( 2,lines))%>%
gtable_add_grob(rectGrob(gp = gpar(fill =gray)),name =border,
t = nrow(g)+ 1,1 = 1,r = ncol(g))%>%
gtable_add_grob(g1,t = nrow(g)+1,l = 1,r = ncol(g))%>%
gtable_add_grob(textGrob(text here,x = 1,hjust = 1,gp = gpar(col =white)),
name =text,t = nrow(g)+ 1,l = 1,r = ncol(g))%>%
grid.draw


I am trying to write a little utility function that will annotate my plots Reuben Fischer-Baum style with a nice little logo and url at the bottom - eg:

I've figured out how to get a postscript file loaded into R; it's justifying the image on the bottom of the plot that's driving me crazy. I can't get the pictureGrob to left justify; at best I can get it to center on the left hand side of my plot, but half of my image gets cut off, like this reproducible example below:

Tinkering with the width setting can sort of approximate what I'm trying to accomplish by limiting the buffer around the pictureGrob, but what I really want, I think, is to left justify the picture itself inside of its viewport OR make the viewport completely snug to the picture. [Paul Murrell does something similar on p. 162 of the grid book, but that seems to depend on a stringWidth utility function that has no partner for pictures(?)]

I've asked a similar question about gridextra tables, but as baptiste comments there, 'Justification is always rather awkward to handle in grid'.

Any help is appreciated; a pointer to any conceptual discussion of what is happening with justification in grid would be spectacular.

Reproducible code (note that grImport requires a ghostscript install):

require(grImport)
require(ggplot2)
require(gridExtra)

#nb: you also need ghostscript installed on your machine.
#I had a hell of a time getting ghostscript to work properly.  
#From the bowels of a R mailing list, I found this helpful tip 
#from Paul Murrell himself:
#https://www.mail-archive.com/r-help@r-project.org/msg203180.html

#YMMV, THIS WORKED ON MY MACHINE:
  #gswin <- shortPathName("C:/Program Files/gs/gs9.15/bin/gswin64c.exe")
  #Sys.setenv(R_GSCMD = gswin)


#get a fun little moon
PostScriptTrace(
  file=file.path(system.file(package = "RGraphics"), "extra", "comic_moon.ps"),
  outfilename="comic_moon.xml")
moon <- readPicture("comic_moon.xml")


annotate_me <- function(
  plot_to_annotate,
  some_picture,
  bg_color="gray30"
) {

  #make a gTree
  foo <- gTree(
    children=gList(
      rectGrob(gp=gpar(fill=bg_color)),
      pictureGrob(
        picture=some_picture,
        x=0, y=0.5,
        #width=?
      )
    )
  )

  final <- arrangeGrob(
    plot_to_annotate, foo,
    nrow=2, heights=c(19,1)
  )

  return(final)
}


annotate_me(
  plot_to_annotate=qplot(mpg, data=mtcars),
  some_picture=moon
)

解决方案

I believe widthDetails isn't implemented for pictureGrobs, so justification does random things.

Here's one approach: define the width of your object, and place it at x = 0.5 width.

require(grImport)
hourglass <- new("Picture",
                 paths= list(new("PictureFill",
                                 x=c(0, 1, 0, 1),
                                 y=c(0, 0, 1, 1))),
                 summary= new("PictureSummary",
                              numPaths=1,
                              xscale=c(0, 1),
                              yscale=c(0, 1)))

g1 <- pictureGrob(hourglass, use.gc=FALSE,x=unit(1,"line"),
                  width=unit(2,"line"), gp=gpar(fill="red"))
library(ggplot2)
library(gtable)
library(dplyr)

p <- qplot(1,1)
g <- ggplotGrob(p) 

grid.newpage()
g %>%
  gtable_add_rows(unit(2,"lines")) %>%
  gtable_add_grob(rectGrob(gp=gpar(fill="grey")), name="border",
                  t=nrow(g)+1,l=1,r=ncol(g)) %>%
  gtable_add_grob(g1, t=nrow(g)+1,l=1,r=ncol(g)) %>%
  gtable_add_grob(textGrob("text here", x=1, hjust=1, gp=gpar(col="white")),
                  name = "text", t=nrow(g)+1,l=1,r=ncol(g)) %>%
  grid.draw

这篇关于538-ify ggplot输出:在R网格图形中验证pictureGrob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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