Display custom image as geom_point [duplicate]

ε祈祈猫儿з 提交于 2019-11-26 22:40:12
bdeb2015

The point geom is used to create scatterplots, and doesn't quite seem to be designed to do what you need, ie, display custom images. However, a similar question was answered here, which indicates that the problem can be solved in the following steps:

(1) Read the custom images you want to display,

(2) Render raster objects at the given location, size, and orientation using the rasterGrob() function,

(3) Use a plotting function such as qplot(),

(4) Use a geom such as annotation_custom() for use as static annotations specifying the crude adjustments for x and y limits as mentioned by user20650.

Using the code below, I could get two custom images img1.png and img2.png positioned at the given xmin, xmax, ymin, and ymax.

library(png)
library(ggplot2)
library(gridGraphics)
setwd("c:/MyFolder/")

img1 <- readPNG("img1.png")
img2 <- readPNG("img2.png")
g1 <- rasterGrob(img1, interpolate=FALSE)
g2 <- rasterGrob(img2, interpolate=FALSE)
qplot(1:10, 1:10, geom="blank") + 
  annotation_custom(g1, xmin=1, xmax=3, ymin=1, ymax=3) +
  annotation_custom(g2, xmin=7, xmax=9, ymin=7, ymax=9) +  
  geom_point()

This doesn't quite do what you want within geom_point but it does perhaps suggest a quick alternative. However, it includes a rather crude adjustment for the x and y limits.

library(png)
library(ggplot2)

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

ggplot(mtcars, aes(mpg, wt)) + 
       geom_blank() +
       mapply(function(xx, yy) 
          annotation_raster(img, xmin=xx-1, xmax=xx+1, ymin=yy-0.2, ymax=yy+0.2),
          mtcars$mpg, mtcars$wt) 

For facets see Kohske's answer on how to alter the mapply function.

EDIT

I think this actually renders better using annotation_custom(), as in Deb's answer. Below allows to loop over all points, rather than having to use individual annotation_custom calls. The slight change from above is that the grob appears to need to be renamed (comment from link)

g <- rasterGrob(img, interpolate=FALSE)

ggplot(mtcars, aes(mpg, wt)) + 
          geom_blank() +
          mapply(function(xx, yy, ii) {
          g$name <- ii
          annotation_custom(g, xmin=xx-1, xmax=xx+1, ymin=yy-0.2, ymax=yy+0.2)},
          mtcars$mpg, mtcars$wt, seq_len(nrow(mtcars))) 

DL Miller provided another solution using ggproto().https://github.com/dill/emoGG

library(ggplot2)
library(grid)
library(EBImage)
img <- readImage(system.file("img", "Rlogo.png", package = "png"))
RlogoGrob <- function(x, y, size, img) {
    rasterGrob(x = x, y = y, image = img, default.units = "native", height = size, 
        width = size)
}

GeomRlogo <- ggproto("GeomRlogo", Geom, draw_panel = function(data, panel_scales, 
    coord, img, na.rm = FALSE) {
    coords <- coord$transform(data, panel_scales)
    ggplot2:::ggname("geom_Rlogo", RlogoGrob(coords$x, coords$y, coords$size, 
        img))
}, non_missing_aes = c("Rlogo", "size"), required_aes = c("x", "y"), default_aes = aes(size = 0.05), 
    icon = function(.) {
    }, desc_params = list(), seealso = list(geom_point = GeomPoint$desc), 
    examples = function(.) {
    })

geom_Rlogo <- function(mapping = NULL, data = NULL, stat = "identity", 
    position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, 
    ...) {
    layer(data = data, mapping = mapping, stat = stat, geom = GeomRlogo, 
        position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
        params = list(na.rm = na.rm, img = img, ...))
}
ggplot(mtcars, aes(wt, mpg))+geom_Rlogo()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!