简体   繁体   中英

R ggplot2: annotation_custom of raster grob does not plot

UPDATE Turns out it was an issue of not being able to produce raster graphics over a remote desktop connection.

I want to insert an image from file into a plot made with ggplot2. This question has already been asked here ( Inserting an image to ggplot2 ) but for me the accepted answer produces a plot devoid of any image. There are no errors or warnings: the axes, grid, and points all plot just fine but there's no R logo ( exported plot ; I couldn't upload an image here). Code below:

library(ggplot2)
library(png)
library(grid)

img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)

qplot(1:10, 1:10, geom="blank") +
  annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
  geom_point()

I've tried playing around with the xy values and have used several different images and formats but to no avail.

The problem I'm having is specific to using a rastergrob object with annotate_custom(), as the examples given on the ggplot2 documentation site for annotate_custom() work just fine, eg:

library(gridExtra)

qplot(1:10, 1:10, geom = "blank") + 
  annotation_custom(grob = tableGrob(head(iris[ ,1:3])),
  xmin = 3, xmax = 6, ymin = 2, ymax = 8)

I'm using 64-bit R version 3.0.2 on windows with ggplot2 version 0.9.3.1 and my packages are all updated.

Any thoughts would be most welcome, and apologies if I'm missing something simple!

As explained in an answer to this question ( R base package grid does not produce output ), "R does not produce raster images correctly in the window device over Remote Desktop Connection. If a raster image is required, the plot must be output to another device."

I was accessing R using a Remote Desktop Connection and thus the raster grob would not plot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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