排除ggplot2上的图例条目中的箭头 [英] Exclude arrowheads in legend entry on ggplot2

查看:141
本文介绍了排除ggplot2上的图例条目中的箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制 geom_path(),我希望它在图中有箭头,但图例中没有箭头。谷歌搜索表明,形式的东西

  guides(color = guide_legend(override.aes = list(..))

会有答案,但我找不到列表的文件)期望 guide_legend()的帮助文件对查看示例进行说明,以了解有关 override的更多信息。 aes ,但只有一个例子显示如何设置图例中的alpha级别。



为了使我想要做的具体,这里是生成带箭头的图的代码,

  data < -  data.frame(x = c(1,1, 1,2),
y = c(1,2,1,1),
color = c('color1','color1','color2','color2'))

library(ggplot2)
ggplot(data,aes(x = x,y = y,color = color,group = color))+
geom_path(size = 2,
箭头=箭头(角度= 30,
长度=单位(0.1,英寸),
()结束=最后,类型=关闭))+
theme_bw()

这输出



但是我在寻找的是不带箭头版本的传说,如

  ggplot(data,aes(x = x,y = y,color = color,group = color))+ 
geom_path(size = 2)+
theme_bw()



谢谢! 理论上,这应该是有效的:

  ggplot(data,aes(x = x,y = y,color = color,group = color))+ 
geom_path(size = 2,
arrow = arrow(angle = 30,
length = u nit(0.1,inches),
ends =last,type =open))+
theme_bw()+
guides(color = guide_legend(override.aes = list (arrow = NULL)))

然而,它没有。



另一种方法是给 GeomPath 一个不绘制箭头的新图例绘制函数:

 #忽略箭头设置的图例绘制函数
#修改自ggplot2函数`draw_key_path`,
#此处可用:https:// github。 com / tidyverse / ggplot2 / blob / 884fdcbaefd60456978f19dd2727ab698a07df5e / R / legend-draw.r#L108
draw_key_line< - function(data,params,size){
data $ linetype [is.na(data $ linetype)] < - 0

grid :: segmentsGrob(0.1,0.5,0.9,0.5,
gp = grid :: gpar(
col = alpha(data $ color ,数据$ alpha),
lwd = data $ size * .pt,
lty = data $ linetype,
lineend =butt


}

#覆盖leg GeomPath
GeomPath $ draw_key< - draw_key_line

ggplot(data,aes(x = x,y = y,color = color,group = color))+
geom_path(size = 2,
arrow = arrow(angle = 30,
length = unit(0.1,inches)),
ends =last,type =closed ))+
theme_bw()



请注意,这会为R会话的其余部分更改 GeomPath 。要恢复原始行为,您可以设置:

  GeomPath $ draw_key<  -  draw_key_path 


I'm drawing a geom_path() and I'd like it to have arrows in the plot, but not have arrows in the legend. Googling around suggests that something of the form

guides(color=guide_legend(override.aes = list(..)) 

will have the answer, but I can't find documentation of what that list() expects. The help file for guide_legend() says to "look at the examples" to learn more about override.aes but there is only one example showing how to set the alpha level in the legend.

To make what I'd like to do concrete, here's code that produces a plot with arrowheads,

data <- data.frame(x=c(1, 1, 1, 2),
                   y=c(1, 2, 1, 1),
                   color=c('color1', 'color1', 'color2', 'color2'))

library(ggplot2)
ggplot(data, aes(x=x, y=y, color=color, group=color)) +
  geom_path(size=2,
            arrow = arrow(angle = 30, 
                          length = unit(0.1, "inches"),
                          ends = "last", type = "closed")) +
  theme_bw()

This outputs

But what I'm looking for is the legend with the not arrowhead version, as in

ggplot(data, aes(x=x, y=y, color=color, group=color)) +
  geom_path(size=2) +
  theme_bw()

Thank you!

解决方案

In theory, this should work:

ggplot(data, aes(x=x, y=y, color=color, group=color)) +
  geom_path(size=2,
            arrow = arrow(angle = 30, 
                          length = unit(0.1, "inches"),
                          ends = "last", type = "open")) +
  theme_bw() +
  guides(color=guide_legend(override.aes = list(arrow = NULL))) 

However, it doesn't.

The alternative is to give GeomPath a new legend-drawing function that doesn't draw an arrow:

# legend drawing function that ignores arrow setting
# modified from ggplot2 function `draw_key_path`,
# available here: https://github.com/tidyverse/ggplot2/blob/884fdcbaefd60456978f19dd2727ab698a07df5e/R/legend-draw.r#L108
draw_key_line <- function(data, params, size) {
  data$linetype[is.na(data$linetype)] <- 0

  grid::segmentsGrob(0.1, 0.5, 0.9, 0.5,
    gp = grid::gpar(
      col = alpha(data$colour, data$alpha),
      lwd = data$size * .pt,
      lty = data$linetype,
      lineend = "butt"
    )
  )
}

# override legend drawing function for GeomPath
GeomPath$draw_key <- draw_key_line

ggplot(data, aes(x=x, y=y, color=color, group=color)) +
  geom_path(size=2,
            arrow = arrow(angle = 30, 
                          length = unit(0.1, "inches"),
                          ends = "last", type = "closed")) +
  theme_bw()

Note that this changes GeomPath for the remainder of your R session. To revert back to the original behavior, you can set:

GeomPath$draw_key <- draw_key_path

这篇关于排除ggplot2上的图例条目中的箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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