Ggploy:geom_vline和geom_hline的独立图例 [英] ggplot: Separate legend for both a geom_vline and geom_hline

查看:19
本文介绍了Ggploy:geom_vline和geom_hline的独立图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为geom_vlinegeom_hline创建一个单独的图例。

以下是我的代码:

set.seed(1)
vec1 = rnorm(20)
set.seed(2)
vec2 = rnorm(20)
set.seed(3)
vec3 = rnorm(20)

vec  = c(vec1,vec2,vec3)
let  = rep(sample(letters[1:3],20,replace=TRUE),3)
num  = c(rep(1:20,3))
lab  = c(rep("label 1",20),rep("label 2",20),rep("label 3",20))

# Vertical lines for geom_vline
line1 = seq(1,20,5)

df   = data.frame(vector = vec, index = num, name = let, label = lab)

# Define facet order
df$label_f = factor(df$label, levels=c("label 1","label 2","label 3"))

# Dataframe for horizontal lines
hl = data.frame(label = c("label 1","label 2","label 3"), Z = c(0.5,1,-0.5))

# Factor to seperate horizontal lines for facets
hl$label_f = factor(hl$label, levels=c("label 1","label 2","label 3"))

ggplot(data=df,aes(index,vector)) + 
geom_point(aes(alpha = name, color=name)) +
geom_vline(xintercept=line1, color="blue") +
geom_hline(data = hl, aes(yintercept=Z),color="red") +
scale_alpha_manual(name="Indices",
                     labels=c("legend label 1","legend label 2","legend label 3"),
                     values=c(1,0.5,0.5)) +
scale_color_manual(name="Indices",
                     labels=c("legend label 1","legend label 2","legend label 3"),
                     values=c("black","orange","darkorange4")) +        
xlab("index") + ylab("rnorm") +  
facet_wrap(~label_f,ncol=1,scale="free_y") +
theme(legend.position="bottom")

这是ggploy:

我希望geom_hlinegeom_vline具有适当标签和颜色的单独图例(&Q;图例2&Q):

我希望图例中的geom_hline是水平的,geom_vline是垂直的!

它应该是这样的:

谢谢!

推荐答案

利用ggnewscale和自定义draw_key函数,可以这样实现:

  1. 可以通过ggnewscale::new_scale_color实现线条的单独图例,这将添加第二个颜色图例。为此,需要将映射同时添加到hlinevline

  2. 棘手之处在于,默认情况下,图例键将同时显示垂直线和水平线。为了覆盖这一点,我添加了一个定制的键标志符号函数,它限定了行的color之一。这意味着如果您更改颜色,您也必须调整条件。

library(ggplot2)

# Custom Key Glyph. Vertical Line if color = "blue". Horizontal Line otherwise
draw_key_cust <- function(data, params, size) {
  if (data$colour == "blue") {
    draw_key_vpath(data, params, size)
  } else {
    draw_key_path(data, params, size)
  }
}

ggplot(data=df,aes(index,vector)) + 
  geom_point(aes(alpha = name, color=name)) +
  scale_alpha_manual(name="Indices",
                     labels=c("legend label 1","legend label 2","legend label 3"),
                     values=c(1,0.5,0.5), guide = guide_legend(order = 1)) +
  scale_color_manual(name="Indices",
                     labels=c("legend label 1","legend label 2","legend label 3"),
                     values=c("black","orange","darkorange4"), guide = guide_legend(order = 1)) +
  ggnewscale::new_scale_color() +
  geom_vline(data = data.frame(xintercept = line1), aes(xintercept = xintercept, color="line1"), key_glyph = "cust") +
  geom_hline(data = hl, aes(yintercept = Z, color="line2"), key_glyph = "cust") +
  scale_color_manual(name="Legend 2",
                     labels=c(line1 ="line 1", line2 = "line 2"),
                     values=c(line1 = "blue", line2 = "red"), 
                     guide = guide_legend(order = 2)) +
  xlab("index") + ylab("rnorm") +  
  facet_wrap(~label_f,ncol=1,scale="free_y") +
  theme(legend.position="bottom")

这篇关于Ggploy:geom_vline和geom_hline的独立图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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