ggplot2:手动添加图例

mts*_*mts 6 r legend ggplot2

如何将任何(不相关的)图例映射到现有的ggplot?

免责声明:请不要恨我.我知道使用'ggplot2'创建图例的最佳方法是正确地映射您的数据,我99%的时间都这样做.然而,我要求的东西通常可以给我任何我想要的传奇.

作为一个例子,我有一个看起来像这样的情节: 在此输入图像描述

从这段代码创建:

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
Run Code Online (Sandbox Code Playgroud)

我想添加一个包含以下内容的图例:

  • 一条红色的,垂直的垂直线,叫做"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"))
Run Code Online (Sandbox Code Playgroud)

我最好的猜测是如何处理这个是创建一些辅助的伪数据temp,绘制/映射在某个地方看不见的地方,然后用于创建图例,但我没有成功得到这样的东西来绘制我的传奇.

再一次,我的想法是如何在现有的情节中添加任何不相关的图例,即没有将原始数据巧妙地映射到情节变量?

San*_*att 5

可以从头开始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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明