R ggplot2: annotation_custom of raster grob does not plot

蹲街弑〆低调 提交于 2020-01-14 14:45:07

问题


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, e.g.:

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!


回答1:


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.



来源:https://stackoverflow.com/questions/20226858/r-ggplot2-annotation-custom-of-raster-grob-does-not-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!