ggplot2:手动添加图例 [英] ggplot2: manually add a legend

查看:1071
本文介绍了ggplot2:手动添加图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I map any (unrelated) legend to an existing ggplot?

免责声明:请不要恨我.我知道用"ggplot2"创建图例的最佳方法是正确映射数据,而我有99%的时间都这样做.但是在这里,我要的是一些可以给我我想要的任何传说的东西.

Disclaimer: please don't hate me. I know the best way to create a legend with 'ggplot2' is to map your data right and I do it 99% of the time. Here however I am asking for something that in general can give me any legend I want.

作为一个例子,我有一个情节,看起来像这样:

As an example I have a plot that looks somewhat like this:

根据以下代码创建:

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

,我想添加一个图例,其中包含:

and I want to add a legend that contains:

  • 一条红色的垂直长线,称为"l1"
  • 黑色实心水平线,称为"l2"
  • 一个名为"l3"的绿色填充块

理想情况下,我会产生这样的内容(前面的伪代码):

ideally I would produce that somewhat like this (pseudo-code ahead):

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,将其绘制/映射到图上不可见的某个地方,然后用于创建图例,但是我没有成功获得这样的东西给我一个传说.

my best guess how to approach this would be to create some auxiliary pseudo-data temp that is plotted/mapped somewhere invisible on the plot and then used to create the legend, but I was not successful in getting anything like this to plot me a legend.

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

Once more, the idea is how can I add any unrelated legend to an existing plot, i.e. without clever mapping of the original data to the plot variables?

推荐答案

可以从头开始构建图例:使用grid构造图例的元素;然后使用gtable将元素放置在图例中,并将图例放置在图形中.这有点粗糙,但是给出了总体思路.

A legend can be constructed from scratch: use grid to construct the elements of legend; then use gtable to position the elements within the legend, and the legend within the plot. This is a bit crude, but gives the general idea.

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)

这篇关于ggplot2:手动添加图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆