2012-10-30 20 views
11

ggplot2に、例えば2行のプロットがあり、伝説には "Sharks"と "Tiger"があります。そのテキストの代わりに鮫/虎の画像が伝説に現れるようにする方法はありますか?ggplot2の凡例にラベルの代わりにイメージを使用

+1

私は、これは不可能であると言うつもりはありませんが、あなたがRに入るすべてのソリューションは、単にPhotoshopのを発射よりもメーリングリストの難しくなる可能性があります。 (しかし、 'grid.raster'を見れば、あなたが始めるかもしれません。) – joran

+0

またはオープンソースの熱狂者のための[gimp](http://www.gimp.org/)。 – Chase

+0

ええ、ラスタプログラムで手作業で行うのは、大掛かりな繰り返しグラフ生成のためのものです。 – nicolaskruchten

答えて

32

図形をepsまたはsvgとして保存してから、Illustratorで開いたり、凡例を画像に置き換えたりする方が、ggsaveを使用する方がずっと良いです。あなたがRでそれをやっていることを本当に諦めているならば、にannotation_rasterを使用して、隣にあるテキストをgeom_textを使って追加することができます。ここでは大まかな試みです:

set.seed(10) 
library(ggplot2) 
library(RCurl) 
library(png) 
df <- data.frame(animal = sample(c("sharks", "tigers"),20, rep=T), time=1:20, 
       scariness = rnorm(20)*-20) 

shark <- readPNG(getURLContent("http://i.imgur.com/EOc2V.png")) 
tiger <- readPNG(getURLContent("http://i.imgur.com/zjIh5.png")) 

ggplot(df, aes(time, scariness, group = animal, color = animal)) + 
geom_line(show_guide = FALSE) + 
annotation_raster(tiger, xmin = nrow(df)-1, xmax = nrow(df), 
    ymin = max(df$scariness)-(.05*max(df$scariness)), 
    ymax = max(df$scariness), interpolate = T) + 
annotation_raster(shark, xmin = nrow(df)-1, xmax = nrow(df), 
    ymin = max(df$scariness)-(.1*max(df$scariness)), 
    ymax = max(df$scariness)-(.05*max(df$scariness)), interpolate = T) 

shark tiger scariness figure

+1

+1それは素晴らしいです。 –

+8

あなたは、1000人のインターネットビールが必要です。またはおそらくただのサメ。それはあなた次第です。 – joran

+0

恐ろしい回避策と例!ありがとう! – nicolaskruchten

関連する問題