0

I am trying to combine the shape and colour legends combined in the plot but all I can do is combine colour and linetype.

sample data:

> dput(sample_n(ALL_Spans, 10))
structure(list(UG = c("100", "124", "108", "92", "124", "84", 
"100", "100", "124", "84"), S = c(12, 12, 12, 12, 12, 12, 12, 
12, 12, 12), EQ = c("Cai", "AASHTO LRFD", "Suksawang & Nassif", 
"AASHTO LRFD", "Modified Henry", "Modified Henry", "Analytical Data", 
"Rigid Rotation", "Rigid Rotation", "Rigid Rotation"), DF = c(0.818313323177077, 
1.01028571424963, 0.741281190719393, 1.08244897959184, 0.858545454545455, 
0.858545454545455, 0.789710362673591, 0.71825, 0.926666666666667, 
0.926666666666667), Girder = c("Exterior Girder", "Exterior Girder", 
"Exterior Girder", "Interior Girder", "Interior Girder", "Exterior Girder", 
"Exterior Girder", "Interior Girder", "Exterior Girder", "Exterior Girder"
), Type = c("Moment LLDF", "Shear LLDF", "Moment LLDF", "Shear LLDF", 
"Moment LLDF", "Moment LLDF", "Moment LLDF", "Moment LLDF", "Moment LLDF", 
"Moment LLDF"), L = c(240L, 300L, 260L, 220L, 300L, 200L, 240L, 
240L, 300L, 200L), group = c("Cai 12", "AASHTO LRFD 12", "Suksawang & Nassif 12", 
"AASHTO LRFD 12", "Modified Henry 12", "Modified Henry 12", "Analytical Data 12", 
"Rigid Rotation 12", "Rigid Rotation 12", "Rigid Rotation 12"
)), row.names = c(NA, -10L), class = "data.frame")

code for the plot:

b <- ggplot(data = ALL_Spans, aes(x = L, y = DF, colour = EQ)) +  
    geom_point(aes(shape = EQ), stroke = 0.2, size = 2) + 
    geom_line(size = 0.2, linetype = "dotted") +
  scale_shape_manual(values = c(0,19,3,2,4,5,6,8)) + theme_classic() +
  scale_colour_manual(values = c("black", "red", rep("black",6))) +
  scale_x_continuous(breaks = seq(200,300,20), limits = c(200,300)) +
      labs(x = "x", y = element_blank(), shape = element_blank()) +
    theme(legend.title.align = 0.5, legend.position = "bottom", 
          axis.text = element_text(size=8, colour="black"),
          axis.title.x = element_text(size=8, colour="black"), 
          axis.line = element_blank(), 
          axis.ticks = element_line(colour = "black", size = 0.2),
          legend.text = element_text(size=8, margin = margin(-10,5,-10,-4.5)),
          legend.title = element_text(size=8, margin = margin(-10,1.5,-10,0)),
          legend.spacing.x = unit(0.1, 'cm'), legend.spacing.y = unit(0, 'cm'),
          legend.background = element_blank(), panel.margin.y = unit(0.3, "lines"),
          legend.box.background = element_rect(color = "black", size = 0.2)) +
  guides(shape = guide_legend(nrow=2,byrow=TRUE), linetype = FALSE,
         color = guide_legend(nrow = 2, byrow = TRUE)) + 
  facet_grid(Type~Girder)

the current plot is

enter image description here

the desired plot would be

enter image description here

Maral Dorri
  • 468
  • 5
  • 17
  • Please provide a [reproducible minimal example](https://stackoverflow.com/q/5963269/8107362). Especially, provide some sample data, e.g. with `dput()` and try to strip down this plot to the minimum of code necessary to reproduce the issue. – mnist Apr 09 '21 at 14:18
  • @mnist I just added sample data and simplified the code for the plot – Maral Dorri Apr 09 '21 at 14:27

1 Answers1

1

ggplot will try to combine legends wherever possible by default; however, you will override this combination if any part of the legend creation, labelling, or guides are differentiated for the two aesthetic modifiers in question. In this case, you have shape and color, which are combined by default. The problem is with this line:

 labs(x = "Span Length (ft)", y = element_blank(), shape = element_blank()) +

You indicate the name for the shape aesthetic here. By default, it's set to waiver(), so this line would set the title of shape=element_blank() and by default the title of color=waiver(). This is a differentiation of the two, so two legends are created. To remove the title from your legend, you need to specify the same to both. To solve your problem, change it to be the following:

 labs(x = "Span Length (ft)", y = element_blank(), shape = element_blank(), color=element_blank()) +

I can demonstate with the following example using mtcars:

p <- ggplot(mtcars, aes(disp, mpg)) +
  geom_point(aes(color=factor(cyl), shape=factor(cyl)))
p

enter image description here

If one changes to element_blank() (or NULL, or anything else...):

p + labs(shape=element_blank())

enter image description here

If you need to remove the legend entirely, you set both to element_blank(). If you need to overall change the legend name, you would have to similarly change both of the legend titles:

p + labs(shape="My Legend Title", color="My Legend Title")

enter image description here

EDIT

OP, thanks for sharing a sample of your data. When I add color=element_blank() to the line in question in addition to shape=element_blank(), you get your desired combined legend:

enter image description here

chemdork123
  • 12,369
  • 2
  • 16
  • 32