3

I've tried to follow the answer's given already for adding images to plots, but they do not work when using coord_polar()

# install.packages("RCurl", dependencies = TRUE)
library(RCurl)
myurl <- "http://vignette2.wikia.nocookie.net/micronations/images/5/50/German_flag.png"

# install.packages("png", dependencies = TRUE)
library(png)
img <-  readPNG(getURLContent(myurl))

# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)

ger <- grid::rasterGrob(img, interpolate=TRUE)

## works, adds the image to the plot
ggplot(mtcars, aes(x=mpg, y= cyl)) + geom_line() + annotation_custom(ger, 10, 15, 5)

## doesn't work
ggplot(mtcars, aes(x=mpg, y= cyl)) + geom_line() + annotation_custom(ger) + coord_polar()
> Error: annotation_custom only works with Cartesian coordinates

Ideally I'd like to be able to position the flag image within the center of the polar plot, and another image along the y-axis.

Is there anyway to add the image? It can be as-is, no transformation required.

I'm using ggplot2 version 2.0

Community
  • 1
  • 1
MarkeD
  • 2,500
  • 2
  • 21
  • 35
  • 1
    It's more likely that we will be able to help you if you make a minimal _reproducible_ example to go along with your question (what is `ex`?). – Eric Fail Dec 28 '15 at 18:11
  • Typo sorry, ex is mtcars – MarkeD Dec 28 '15 at 18:14
  • 3
    Do you want the images to be transformed from cartesian to polar coordinates as well? Or do you want the image, as-is, to be added to the top of the polar plot? If the latter, the `cowplot` package might do what you need. – Gregor Thomas Dec 28 '15 at 18:25
  • The image to be as-is. I'll add to question, thanks – MarkeD Dec 28 '15 at 18:26

1 Answers1

5

Gregor's suggestion for using the cowplot library has got me there.

Following the introduction to cowplot you can use the draw_grob function to overlay images as you like. It takes a bit of tweaking as the position changes depending on the dimensions of the plot, but its possible.

Example:

# install.packages("RCurl", dependencies = TRUE)
library(RCurl)
myurl <- "http://vignette2.wikia.nocookie.net/micronations/images/5/50/German_flag.png"

# install.packages("png", dependencies = TRUE)
library(png)
img <-  readPNG(getURLContent(myurl))

# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)

ger <- grid::rasterGrob(img, interpolate=TRUE)

g <- ggplot(mtcars, aes(x=mpg, y= cyl)) + geom_line() + coord_polar()

# install.packages("cowplot", dependencies = TRUE)
library(cowplot)
h <- ggdraw(g)
## tweak this to fit your plot
h + draw_grob(ger, 0.4, 0.48, 0.07, 0.07)

enter image description here

Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
MarkeD
  • 2,500
  • 2
  • 21
  • 35
  • Draw_grob does nothing more than calling annotation_custom: > draw_grob function (grob, x = 0, y = 0, width = 1, height = 1) { annotation_custom(grid::grobTree(grob), xmin = x, xmax = x + width, ymin = y, ymax = y + height) } it also fails when plotting on a ggmap – Xavier Prudent Nov 30 '16 at 19:19