[R] Add png image outside plot borders

Joshua Wiley jwiley.psych at gmail.com
Mon Sep 19 00:41:02 CEST 2011


Great, thanks Amelia.  Here is what I would do:

require(png)
require(grid)

## select the PNG file
z <- readPNG(file.choose())

## Function to draw your statement
copyright.draw <- function(label, image, x, y, size, ...) {
  lab <- textGrob(label = label,
    x = unit(x, "npc"), y = unit(y, "npc"),
    just = c("left", "centre"), gp = gpar(...))
  logo <- rasterGrob(image = image,
    x = unit(x, "npc") + unit(1, "grobwidth", lab), y = unit(y, "npc"),
    width = unit(size, "cm"), height = unit(size, "cm"),
    just = c("left", "centre"), gp = gpar(...))
  grid.draw(lab)
  grid.draw(logo)
}

plot(1:10, 1:10)
copyright.draw("Copyright statement ", z, .02, .04, .5, fontsize = 8)

Seems reasonably aesthetically pleasing to me (don't forget the space
after your copyright statement if you want there to be a space there
before the CC logo).

I kind like Baptiste's idea to use the SVG file directly, but you
asked for PNG.  Also, the function is not strictly necessary, what is
nice about it is that it handles positioning the logo based on the
text.  You don't want to be in a position where you are trying to
manually line the two up.  You can pass additional arguments as you
like to gpar (though note it will affect botht he text and raster
grobs.

Cheers,

Josh

On Sun, Sep 18, 2011 at 1:59 PM, Amelia McNamara
<amelia.mcnamara at stat.ucla.edu> wrote:
> If you run this, you'll see that I have some text at the bottom, but
> the logo is within the plot borders.
>
> plot(c(1.1, 2.3, 4.6), c(2.0, 1.6, 3.2), ylab="", xlab="")
> mtext("X axis label", side=1, line=3)
> mtext("Copyright statement", side=1, line=4, adj=0, cex=0.7)
> library(png)
> z <- readPNG("Cc.logo.circle.png")
> rasterImage(z, 1, 1.6, 1.2, 1.7)
>
> I've tried doing things like
>
> rasterImage(z, 1, 0.5, 1.2, 1)
>
> but nothing shows up. The documentation for rasterImage() says that
> the corner values have to be within the plot region. As I said before,
> I want the logo to be down on the level of my copyright text, outside
> the plot region.
>
> Thanks!
>
>
> On Sun, Sep 18, 2011 at 1:26 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
>> Hi Amelia,
>>
>> Can you give an example (using text where you want the CC is fine)?
>> Two angles I would try would be A) changing the regions or related but
>> more flexible (and hence complex) B) use grid of course if you're
>> making these with, say, ggplot2, you're already in grid (but then
>> mtext probably would not work, though I have not tried it offhand).
>> Anyway, an example (code please, not just the picture), will clear up
>> all these questions and we can offer a solution tailored to what you
>> are doing.
>>
>> Cheers,
>>
>> Josh
>>
>> On Sun, Sep 18, 2011 at 1:18 PM, Amelia McNamara
>> <amelia.mcnamara at stat.ucla.edu> wrote:
>>> I am trying to add a copyright disclaimer outside the plot borders of
>>> some images I have created. I can use mtext() to add the written
>>> portion, but I would like to have the Creative Commons license image
>>> (http://en.wikipedia.org/wiki/File:Cc.logo.circle.svg) before the
>>> text. I've found that I can plot a .png image inside the plot
>>> boundaries using rasterImage() but I can't figure out how to do it
>>> outside the boundaries.
>>>
>>> Any help would be great. If you know unicode or Adobe Symbol encoding
>>> for the CC logo, that might work too.
>>>
>>> ~Amelia McNamara
>>> Statistics PhD student, UCLA
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>> --
>> Joshua Wiley
>> Ph.D. Student, Health Psychology
>> Programmer Analyst II, ATS Statistical Consulting Group
>> University of California, Los Angeles
>> https://joshuawiley.com/
>>
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list