2

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()

enter image description here

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'))

enter image description here

Any help? Thanks!

1 Answers1

5

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: enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • 1
    Thanks @Jaap! This worked out. I just changed the order inside the list get it right ('dotted' and 'dashed' is in wrong place). – André Tavares Apr 29 '15 at 15:35
  • @Jaap why do we need to define the linetype vector here? Can we put any default values here or do they need to be line types like "dashed"? Also how do I prevent this from overriding the labels I had set in scale_linetype_discrete? – user43953 Dec 19 '19 at 18:49