简体   繁体   English

将图像合并为ggplot2图的图例

[英]Incorporating an image as legend for ggplot2 plot

I have an image that I want to use as a custom legend - a universal legend of 3 plots in one figure combined with grid.arrange: 我有一个我想用作自定义图例的图像 - 一个图中3个图的通用图例与grid.arrange相结合:

自定义传奇

I figure I can load this into R with png or some such package and use grid.arrange to place it next to my graphs. 我想我可以用png或一些这样的包加载到R中,并使用grid.arrange将它放在我的图表旁边。 Is it possible to include such images in the package that already contains the functions to make my combined chart, and if so where does one put it and how would you call it from within R code then from it's location within the package. 是否可以在已包含用于制作组合图表的函数的包中包含此类图像,如果是,则将其放在何处以及如何从R代码中调用它,然后从包中的位置调用它。 I intend to use this sort of graph with this legend often. 我打算经常在这个图例中使用这种图形。

UPDATE: 更新:

I've created a folder called 'img' in the root of my Package structure and placed the file inside. 我在Package结构的根目录中创建了一个名为'img'的文件夹,并将文件放在里面。

In my R code is the line: legend <- readPNG(system.file("img", "rgblegend.png", package="HybRIDS"), TRUE) However when build the binary, then install it from local zip file, I go to plot and: 在我的R代码中是行: legend <- readPNG(system.file("img", "rgblegend.png", package="HybRIDS"), TRUE)但是在构建二进制文件时,从本地zip文件安装它,我去绘图并且:

Error in readPNG(system.file("img", "rgblegend.png", package = "HybRIDS"), : unable to open If I check my library there is no folder 'img' Error in readPNG(system.file("img", "rgblegend.png", package = "HybRIDS"), : unable to open如果我检查我的库没有文件夹'img'

EDIT: 编辑:

I've checked a source version of my package - it includes the 'img' folder, however installing from source also fails to put the folder into my directory. 我检查了我的软件包的源版本 - 它包含'img'文件夹,但是从源代码安装也无法将文件夹放入我的目录。 So I guess my question now is, why does making a binary package not include the img folder, and why does the source package include it but not install it to my library - what do I do to correct this? 所以我想我现在的问题是,为什么制作二进制包不包含img文件夹,为什么源包包含它但不安装到我的库 - 我该怎么做才能纠正这个问题?

agstudy's answer describes how to reference a file in an installed package without having to know where the package was installed. agstudy的答案描述了如何在已安装的软件包中引用文件,而无需知道软件包的安装位置。 The other part of the problem is getting the image file into the installed package. 问题的另一部分是将映像文件放入已安装的软件包中。

From the "Package subdirectories" second of Writing R Extensions , the section of interest is about the inst subdirectory: Writing R Extensions的“Package子目录”第二部分开始,感兴趣的部分是关于inst子目录:

The contents of the inst subdirectory will be copied recursively to the installation directory. inst子目录的内容将以递归方式复制到安装目录中。 Subdirectories of inst should not interfere with those used by R (currently, R , data , demo , exec , libs , man , help , html and Meta , and earlier versions used latex , R-ex ). inst子目录不应该干扰R使用的那些(目前, RdatademoexeclibsmanhelphtmlMeta ,以及使用latexR-ex早期版本)。 The copying of the inst happens after src is built so its Makefile can create files to be installed. 在构建src之后发生了对inst的复制,因此其Makefile可以创建要安装的文件。

So for a file to appear in the img directory off the root of the installed package, it must appear in the inst/img directory of the source. 因此,对于一个文件出现在已安装包的根目录下的img目录中,它必须出现在源的inst/img目录中。 So move rgblegend.png from img to inst/img and then 所以将rgblegend.pngimg移动到inst/img然后

legend <- readPNG(system.file("img", "rgblegend.png", package="HybRIDS"), TRUE)

should work. 应该管用。

If I understand the question is how you can be sure to find the image within the package. 如果我理解的问题是如何确保在包装内找到图像。 You can put the png under a folder img of your package. 您可以将png放在包的img文件夹下。 and call it using something like this : 并使用以下内容调用它:

img <- readPNG(system.file("img", "Zwrch.png", package=package_name))

The complete solution looks to something like this: 完整的解决方案看起来像这样:

library(ggplot2)     
library(grid)        ## to create the image grobe
library(png)         ## to read the png
library(gridExtra)   ## to arrange the plots
bp <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()
img <- readPNG(system.file("img", "Zwrch.png", package=package_name))
legend <- rasterGrob(image=img)
grid.arrange(bp,legend)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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