1

I need the legend in my plot to reflect the color and linetype in the graph. My code is below.

    tibble::tribble(
      ~variable,           ~value,  ~bin,              ~ci,
  "Advanced HS", 302.097396142857, "FFD", 21.2978862705524,
  "Beginner HS", 394.264259996296, "FFD", 28.8027249069651,
     "Children", 379.642674397368, "FFD", 18.1482664147856,
       "Adults", 217.875580083333, "FFD", 2.38006646944708,
  "Advanced HS", 305.244904223809, "SFD", 15.5487255740993,
  "Beginner HS", 350.108404085185, "SFD", 28.6379128865829,
     "Children", 358.329722218421, "SFD", 12.6900032541364,
       "Adults", 228.522062813542, "SFD", 2.73230976574123,
  "Advanced HS", 484.181295019048,  "GD", 33.9933197272878,
  "Beginner HS", 960.745730144444,  "GD", 55.8118108393556,
     "Children", 676.158669963158,  "GD",  44.238558449867,
       "Adults", 259.127882341667,  "GD", 4.30648424130776,
  "Advanced HS", 702.136573061905,  "TT", 47.0883793662974,
  "Beginner HS", 1541.69112013704,  "TT", 84.3091269009313,
     "Children",  976.54035988421,  "TT", 60.0584639291095,
       "Adults",  318.08245218125,  "TT", 7.99092674935576
  )



mytheme <- theme_bw() + theme(axis.title = element_text(size = rel(2)),
                          strip.text = element_text(size = rel(2)),
                          legend.position = "bottom",
                          legend.key = element_blank(),
                          text = element_text(size = 28),
                          legend.text = element_text(size = 28), 
                          plot.margin = margin(20, 20, 20, 20),
                          axis.text.x = element_text(hjust = 0.5, vjust = 0.5, size = 35),
                          axis.text.y = element_text(hjust = 0.5, vjust = 0.5, size = 35),
                          strip.background = element_rect(fill = 'gray96'))


tiff("r.tiff", units="in", width=10, height=7, res=300)
                         ggplot(data = Figure_1) + 
                         mytheme  + scale_y_continuous(breaks = seq(0,2000, 500))+ 
                         aes(x = bin, y = value, group = variable, color = variable) + scale_x_discrete(limits = c ("FFD","SFD", "GD", "TT")) +
                         geom_errorbar(aes(ymin=value-ci, ymax=value+ci), width=.1) + 
                         geom_line(aes(group=variable, linetype = variable), size =2) + scale_linetype_manual(values=c("solid", "longdash", "dotdash", "dotted")) + 
                         theme(legend.title = element_blank()) +
                         guides(col = guide_legend(reverse = FALSE, override.aes = list(size=6))) +
                         xlab("") + 
                         ylab("Duration (ms)")
                         dev.off()
Michael Ohlrogge
  • 10,559
  • 5
  • 48
  • 76

1 Answers1

1

Your linetypes not showing up in the legend is some combination of

  1. the geom_errorbar layer is using linetype='solid' which is over-drawing or over-writing (I am not sure which) the linetype from the geom_line layer
  2. the size of the lines in the legend is so big that the linetype cannot be seen

To fix (1), use show.legend=FALSE in geom_errobar

geom_errorbar(aes(ymin=value-ci, ymax=value+ci), width=.1, show.legend=FALSE)

At this point, this will make no apparent difference to the resulting image because of the second issue (2): the lines are so big in the legend/the space to display the pattern is so small that even one dot of the purple dotted line consumes the entire space allocated to it (no space to show a second dot).

To fix this there are a few options which you will have to decide between based on aesthetics:

  • If you also change your guides line to have size=1 instead of size=6 you will at least see the dotted and dot-dash pattern in the legend, but still won't see a difference between the dash-dash and solid linetypes because again the space to show the line is not big enough to show the pattern. (plus given the scale of everything else in the graphic I suspect size=1 would be unacceptably small in the finished graph).
  • You can + theme(legend.key.width=units(5, 'lines')) in combo with guides(..., size=2) to make the bit of the legend that shows the lines wider (while still having thick enough lines to see), in order to show enough of a repeat of the pattern, but this pushes the last element of the legend off the size of your image (shown below) so you might need to increase the image size (or reduce the legend font size) smaller to fit. NB default legend.key.width is unit(1.2, 'lines') in theme_bw() so I chose the 5 by trial and error.

    enter image description here

    • you can also guides(lty = guide_legend(reverse = FALSE, override.aes = list(size=2), byrow=TRUE, ncol=2)) to get the legend to wrap onto two lines, though then perhaps you should stick with the original vertically-oriented legend on the right-hand side of the graph. Note I had to change col= to lty= (they are a combined legend anyway) enter image description here
  • as a general observation, for this specific figure, it looks like the colour already identifies the lines so perhaps it is acceptable to only show the colour on the legend and omit the linetype (though then when printing black and white it will be difficult to distinguish them which may be the reason you put the linetype in in the first place)

mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194