Plot data over background image with ggplot

后端 未结 1 1324
走了就别回头了
走了就别回头了 2020-12-01 14:32

I\'m trying to plot some data over a background image. The problem is that both layers end up using the same scale. This is unfortunately problematic.

An example.<

相关标签:
1条回答
  • 2020-12-01 15:13

    Try this, (or alternatively annotation_raster)

    library(ggplot2)
    library(jpeg)
    library(grid)
    
    img <- readJPEG("image.jpg")
    
    df <- data.frame(x=sample(1:64, 1000, replace=T), 
                     y=sample(1:64, 1000, replace=T))
    
    ggplot(df, aes(x,y)) + 
      annotation_custom(rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc")), 
                        -Inf, Inf, -Inf, Inf) +
      stat_bin2d() +
      scale_x_continuous(expand=c(0,0)) +
      scale_y_continuous(expand=c(0,0)) 
    

    screenshot

    0 讨论(0)
提交回复
热议问题