在ggplot中使用geom_vline()复制图例 [英] Duplicate legend with geom_vline() in ggplot

查看:949
本文介绍了在ggplot中使用geom_vline()复制图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  aux_graf = structure(list(lines = structure(c(2L,2L, 1L,3L),.Label = c(h0,
ic,median),class =factor),values = c(21.19755,23.06978,
24,22.13366) ),.Names = c(lines,values),row.names = c(NA,
-4L),class =data.frame)

dadosGraf =结构(列表(dados = c(18.7997,20.5035,18.6214,19.9192,
21.117,20.8353,17.527,17.078,17.6197,21.4255,18.7545,19.2026,
18.4187,20.7641,21.0553,17.5905,18.7561, 18.9772,20.3084,
18.8988,19.1688,19.2898,22.559,18.5854,17.8896,21.1609,
26.1371,21.4737,30.9934,22.8421,24.4133,20.4137,25.5475,
21.8791,2.26706,24.7531, 25.7219,22.6389,26.2308,26.7998,
28.4708,26.9941,25.1489,24.6179,27.0194,25.0589,22.1119,
20.3069,23.6758,27.1201,29.6136,25.9948,18.223,23.7336,
22.4208) ,y2 = 1:55),.Names = c(dados,y2),class =data.frame,row.names = c(NA,
-55L))

gp2 < - ggplot(data = aux_graf)+
geom_vline(aes(xintercept = values,color = lines,linetype = lines),show_guide = TRUE)+
geom_point = dadosGraf,aes(x = dados,y = y2),size = 3.2,shape = 21,color ='black',fill ='gray')+
theme_bw()



现在,我只想更改标签,颜色和线型,但是当我这样做时,它会添加另一个传说,我不知道为什么。

  gp2 + scale_color_manual(name ='',labels = c(ic ='Limites IC',median ='Mediana',h0 =' Hipotese Nula'),values = c(ic ='red',median ='black',h0 ='grey40'))
+ scale_linetype_manual(values = c(ic ='dashed',median ='solid' ,h0 ='dotted'))



有什么帮助?谢谢!

解决方案

您可以使用 override.aes 线型图例。 With:

  gp2 + 
scale_color_manual(name ='',
labels = c(ic =' Limites IC',median ='Mediana',h0 ='Hipotese Nula'),
values = c(ic ='red',median ='black',h0 ='grey40'))+
(color = guide_legend(override.aes = list(linetype = c('dotted','dashed','solid'))),
linetype = FALSE)

你会得到这个:


I want to create this graphic:

aux_graf = structure(list(lines = structure(c(2L, 2L, 1L, 3L), .Label = c("h0", 
"ic", "median"), class = "factor"), values = c(21.19755, 23.06978, 
24, 22.13366)), .Names = c("lines", "values"), row.names = c(NA, 
-4L), class = "data.frame")

dadosGraf = structure(list(dados = c(18.7997, 20.5035, 18.6214, 19.9192, 
21.117, 20.8353, 17.527, 17.078, 17.6197, 21.4255, 18.7545, 19.2026, 
18.4187, 20.7641, 21.0553, 17.5905, 18.7561, 18.9772, 20.3084, 
18.8988, 19.1688, 19.2898, 22.059, 18.5854, 17.8896, 21.1609, 
26.1371, 21.4737, 30.9934, 22.8421, 24.4133, 20.4137, 25.5475, 
21.8791, 22.6706, 24.7531, 25.7219, 22.6389, 26.2308, 26.7998, 
28.4708, 26.9941, 25.1489, 24.6179, 27.0194, 25.0589, 22.1119, 
20.3069, 23.6758, 27.1201, 29.6136, 25.9948, 18.223, 23.7336, 
22.4208), y2 = 1:55), .Names = c("dados", "y2"), class = "data.frame", row.names = c(NA, 
-55L))

gp2 <- ggplot(data = aux_graf)+
       geom_vline(aes(xintercept = values, colour=lines, linetype=lines), show_guide=TRUE)+
       geom_point(data = dadosGraf, aes(x = dados, y=y2), size=3.2, shape=21, colour = 'black', fill = 'grey')+
       theme_bw()

Now, i just want to change the labels, colour and linetype, but when I do, it adds another legend and I don't know why.

gp2 + scale_color_manual(name = '', labels = c(ic = 'Limites I.C.', median = 'Mediana', h0 = 'Hipotese Nula'), values = c(ic = 'red', median = 'black',h0 = 'grey40'))
    + scale_linetype_manual(values=c(ic = 'dashed', median = 'solid',h0 = 'dotted'))

Any help? Thanks!

解决方案

You can use override.aes in combination with hiding the linetype legend. With:

gp2 + 
  scale_color_manual(name = '', 
                     labels = c(ic = 'Limites I.C.', median = 'Mediana', h0 = 'Hipotese Nula'),
                     values = c(ic = 'red', median = 'black',h0 = 'grey40')) +
  guides(color = guide_legend(override.aes = list(linetype = c('dotted', 'dashed', 'solid'))),
         linetype = FALSE)

you will get this:

这篇关于在ggplot中使用geom_vline()复制图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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