2013-05-07 35 views
12

背景画像上にいくつかのデータをプロットしようとしています。問題は、両方のレイヤーが同じスケールで終了することです。これは残念ながら問題です。ggplotを使用して背景画像にデータをプロット

例。

これ以上のデータをプロットしたいです。image

sample image

右。だから私はそれをggplotにプロットします。

img <- readJPEG("image.jpg") 
image <- apply(img, 1:2, function(v) rgb(v[1], v[2], v[3])) 
image <- melt(image) 
ggplot(image, aes(row, -column, fill=fill)) + geom_tile() + scale_fill_identity() 

これはうまくいきます。だから、上にいくつかのデータを追加しましょう。

df <- data.frame(x=sample(1:64, 1000, replace=T), 
    y=sample(1:64, 1000, replace=T)) 
ggplot(df, aes(x,y)) + stat_bin2d() 

サンプルデータをプロットすると、thisが得られます。 enter image description here

このデータプロットをグラデーションイメージの上に重ねてください。

ggplot(image, aes(row, -column, fill=fill)) + geom_tile() + 
    scale_fill_identity() + geom_point(data=df2, aes(x=x, y=-y)) 

しかし、それは第二フィルスケールはエラーをスロー指定しようとするとthis

enter image description here

のように終わります。私はthisはできないと言いますが、回避策があるか、または私が見落としているものがあることを期待しています。

+0

[これ](http://kohske.wordpress.com/2010/12/26/use-image-for-background(あるいはannotation_raster又は)、これを試してみてください-in-ggplot2 /)の投稿は古く、新しいバージョンのggplotを更新する必要がありますが、少なくとも回避方法を提案するかもしれません。 – joran

答えて

17

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

これは 'coord_polar'では機能しませんが、この問題の解決策を見つけることができますhttp://stackoverflow.com/questions/34496000/trying-to-add-an-image-to-a-極プロット - エラー - アノテーション - カスタムオンリーワーク。 – Deleet

関連する問題