首页 文章

将自定义图像显示为geom_point [重复]

提问于
浏览
20

这个问题在这里已有答案:

是否可以在R ggplot中将自定义图像(例如png格式)显示为geom_point?

library(png)
pic1 <- readPNG("pic1.png")

png("Heatmap.png", units="px", width=3200, height=3200, res=300)
ggplot(data_frame, aes(medium, day, fill = Transactions))  +
   geom_tile(colour="white")  +
   facet_grid(dime3_year~dime3_month) + 
   scale_fill_gradient(high="blue",low="white") +
   theme_bw() + 
   geom_point(aes(dime3_channel, day, size=Conv,alpha=Conv,image=(annotation_raster(pic1,xmin=0,ymin=0,xmax=5,ymax=5)),color="firebrick")) +

给出错误:

不知道如何自动选择proto / environment类型的对象的比例 . 默认为连续错误:美学必须是长度为1或与dataProblems相同的长度:( annotation_raster(conv_pic,xmin = 0,ymin = 0,xmax = 5,ymax = 5))

3 回答

  • 12

    点geom用于创建散点图,并且似乎不是设计用于执行所需的操作,即显示自定义图像 . 但是,回答了类似的问题here,这表明问题可以通过以下步骤解决:

    (1)读取要显示的自定义图像,

    (2)使用 rasterGrob() 函数渲染给定位置,大小和方向的栅格对象,

    (3)使用 qplot() 等绘图功能,

    (4)使用 annotation_custom() 等geom作为静态注释,指定user206提到的x和y限制的粗略调整 .

    使用下面的代码,我可以得到两个自定义图像img1.png和img2.png位于给定的xmin,xmax,ymin和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()
    
  • 12

    这在 geom_point 中并没有完全符合您的要求,但它可能暗示了一种快速的选择 . 但是,它包括对 xy 限制的相当粗略的调整 .

    library(png)
    library(ggplot2)
    
    img <- readPNG(system.file("img", "Rlogo.png", package="png"))
    
    ggplot(mtcars, aes(mpg, wt)) + 
           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)
    

    enter image description here

    有关方面,请参阅Kohske's answer了解如何更改 mapply 功能 .

    EDIT

    我认为这实际上使用 annotation_custom() 更好,就像Deb的回答一样 . 下面允许遍历所有点,而不是必须使用单独的annotation_custom调用 . 从上面稍微改变一下,grob似乎需要重命名(comment from link

    g <- rasterGrob(img, interpolate=FALSE)
    
    ggplot(mtcars, aes(mpg, wt)) + 
           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)))
    
  • 6

    DL Miller使用 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()
    

相关问题