2016-01-16 81 views
6

(関連性のない)凡例を既存のggplotにどのようにマップできますか?ggplot2:手動で凡例を追加する

免責事項:私を嫌ってはいけません。私は 'ggplot2'を使って伝説を作成する最良の方法があなたのデータを正しくマップすることを知っており、私は99%の時間を費やしています。ここで私は一般的に私が望む伝説を私に与えることができるものを求めています。

例として、私は多少このようになりますプロットがあります。このコードから作成された enter image description here

set.seed(42) 
temp1 = cbind.data.frame(begin = rnorm(10, 0, 1), end = rnorm(10, 2, 1), y1 = 1:10, y2 = 1:10, id = as.character(1:10)) 
temp2 = cbind.data.frame(x = 0:2, y = 1:3*2) 
temp3 = cbind.data.frame(x = seq(0.5, 1.5, 0.33)) 
temp = c() 
plot1 = ggplot(data = temp, aes(x = x)) + 
    geom_vline(data = temp3, aes(xintercept = x), color = "red", linetype = "longdash") + 
    geom_segment(data = temp1, aes(y = y1, yend = y2, x = begin, xend = end, color = id)) + 
    geom_point(data = temp2, aes(x = x, y = y), shape = 4, size = 4) + 
    scale_color_discrete(guide = F) 
plot1 

と私が含まれている伝説追加したい:

  • "l1"と呼ばれる赤い長辺の垂直線
  • "l2"と呼ばれる黒い実線の水平線
  • 「L3」

と呼ばれる緑いっぱいのブロック、理想的に私はややこの(先に擬似コード)のようなものを生成する:これにアプローチする方法

plot2 = plot1 + guide(elements = list(list(type = "line", color = "red", linetype = "longdash", direction = "vertical", label = "l1"), list(type = "line", label = "l2"), list(type = "rect", fill = "green", label = "l3")) 

私の最高の推測を作成することですいくつかの補助的な擬似データtempはプロットされているか、またはプロット上で見えない場所にマッピングされていて、凡例を作成するために使用されていましたが、私はこのようなことで凡例をプロットするのに成功しませんでした。

もう一度、既存のプロットに関連のない凡例を追加するにはどうすればいいですか?つまり、元のデータをプロット変数に巧妙にマッピングしないでください。

+0

は手動 'geom_text'と' geom_line'を使って何かを構築することができます。 –

+0

または、手動で 'grid'とgrobsで何かを追加してください。または、ggplot2に拡張機能を書き込んでみてください。私は、同様の質問がたくさん出てくるので、誰かが最終的に後者をやることを想像します。 –

+0

あなたは「不正行為」して、実際にプロットに表示されない塗りつぶしを追加してから、塗りつぶしラベルが好きなように編集することができます。 – user5029763

答えて

3

凡例はゼロから構築できます。gridを使用して凡例の要素を作成します。次にgtableを使用して、凡例内の要素とプロット内の凡例を配置します。これは少し原油ですが、一般的な考え方を示しています。

set.seed(42) 
temp1 = cbind.data.frame(begin = rnorm(10, 0, 1), end = rnorm(10, 2, 1), y1 = 1:10, y2 = 1:10, id = as.character(1:10)) 
temp2 = cbind.data.frame(x = 0:2, y = 1:3*2) 
temp3 = cbind.data.frame(x = seq(0.5, 1.5, 0.33)) 
temp = c() 

library(ggplot2) 
library(grid) 
library(gtable) 

plot1 = ggplot(data = temp, aes(x = x)) + 
    geom_vline(data = temp3, aes(xintercept = x), color = "red", linetype = "longdash") + 
    geom_segment(data = temp1, aes(y = y1, yend = y2, x = begin, xend = end, color = id)) + 
    geom_point(data = temp2, aes(x = x, y = y), shape = 4, size = 4) + 
    scale_color_discrete(guide = F) 


# Construct the six grobs - three symbols and three labels 
L1 = linesGrob(x = unit(c(.5, .5), "npc"), y = unit(c(.25, .75), "npc"), 
    gp = gpar(col = "red", lty = "longdash")) 
L2 = linesGrob(x = unit(c(.25, .75), "npc"), y = unit(c(.5, .5), "npc")) 
L3 = rectGrob(height = .5, width = .5, gp = gpar(fill = "green", col = NA)) 
T1 = textGrob("l1", x = .2, just = "left") 
T2 = textGrob("l2", x = .2, just = "left") 
T3 = textGrob("l3", x = .2, just = "left") 

# Construct a gtable - 2 columns X 4 rows 
leg = gtable(width = unit(c(1,1), "cm"), height = unit(c(1,1,1,1), "cm")) 
leg = gtable_add_grob(leg, rectGrob(gp = gpar(fill = NA, col = "black")), t=2,l=1,b=4,r=2) 

# Place the six grob into the table 
leg = gtable_add_grob(leg, L1, t=2, l=1) 
leg = gtable_add_grob(leg, L2, t=3, l=1) 
leg = gtable_add_grob(leg, L3, t=4, l=1) 
leg = gtable_add_grob(leg, T1, t=2, l=2) 
leg = gtable_add_grob(leg, T2, t=3, l=2) 
leg = gtable_add_grob(leg, T3, t=4, l=2) 

# Give it a title (if needed) 
leg = gtable_add_grob(leg, textGrob("Legend"), t=1, l=1, r=2) 

# Get the ggplot grob for plot1 
g = ggplotGrob(plot1) 

# Get the position of the panel, 
# add a column to the right of the panel, 
# put the legend into that column, 
# and then add another spacing column 
pos = g$layout[grepl("panel", g$layout$name), c('t', 'l')] 
g = gtable_add_cols(g, sum(leg$widths), pos$l) 
g = gtable_add_grob(g, leg, t = pos$t, l = pos$l + 1) 
g = gtable_add_cols(g, unit(6, "pt"), pos$l) 

# Draw it 
grid.newpage() 
grid.draw(g) 

enter image description here

+0

これはナッツです!しかし、そのような明確な方法でそれを行う方法を示してくれてありがとう。 – bdemarest

関連する問題