Stack Exchange
Missing some legend items when using scale_color_binned(guide = guide_legend())
I use the following code to plot line graphs. When I convert the continuous color legend to discrete one, some of the legend keys are missing. 
df <- data.frame(L = seq(from=30, t =300, by=10), Kg = rep(22427983, length=28),
                 t = rep(10, length=28), de = rep(3.33, length=28))
S <- c(8,10,12,14,16)
df <- merge(df,S)
colnames(df)[5] <- "S"

Span_M.E.M <- data.frame(df, Current = (0.77 + (df$de/9.1)) * (0.075 + (df$S/9.5)^0.6 *
                         (df$S/df$L)^0.2 * (df$Kg/(12 * df$L * (df$t)^3))^0.1), Updated =
                          0.1 + 1.15 * (0.77 + (df$de/9.1)) * (0.075 + (df$S/9.5)^0.6 *
                         (df$S/df$L)^0.2 * (df$Kg/(12 * df$L * (df$t)^3))^0.1))
ggplot(Span_M.E.M, aes(L, colour = S, group = S)) +
 geom_line(aes(y = Current, linetype = "Current")) +
 geom_line(aes(y = Updated, linetype = "Updated")) +
 scale_color_gradient() +
 labs(x = element_text("Span Length (ft)"), y = element_blank(), 
         title = paste("Moment Live Load Distribution Factors \n Exterior Girder - Multi Lane"),
      linetype = element_blank(), color = "Spacing (ft):") +
  scale_linetype_manual(values = c(1, 2),
                        labels = c("Updated","Current")) +
    theme_classic() +
    theme(plot.title = element_text(hjust = 0.5, margin = margin(45,0,20,0), size = 18),
          legend.position="top",
          legend.box.background = element_rect(colour = "black", size = 0.5),
          legend.box.margin = margin(0,0,0,0), legend.background = element_blank()) +
  scale_color_binned(guide = guide_legend(), type = "viridis")

There should be 5 different colors showing but only three are printed in the legend. 


I can't reproduce your code, but have you tried scale_colour_viridis_d(guide = guide_legend(), breaks = unique(Span_M.E.M$S)) instead of your current colour scale?

Thank you so much, @teunbrand I have updated the data to be fully reproducible. I get an error regarding the color being continuous.

Maybe it's because you are using the group = S aesthetic and it's the same as color. Try removing group.

@RuiBarradas, I just tried that, I am still facing the same issue


Remove scale_color_gradient 2) Add breaks = S to the call to scale_color_binned.


@RuiBarradas Just adding breaks = S was enough to resolve this thank you so much, if you want to make a post I will choose it as the answer.

Try this:
library(ggplot2)

ggplot(Span_M.E.M, aes(L, colour = S, group = S)) +
  geom_line(aes(y = Current, linetype = "Current")) +
  geom_line(aes(y = Updated, linetype = "Updated")) +
  scale_colour_continuous(guide = guide_legend(), type = "viridis") +
  labs(x = element_text("Span Length (ft)"), y = element_blank(), 
       title = paste("Moment Live Load Distribution Factors \n Exterior Girder - Multi Lane"),
       linetype = element_blank(), color = "Spacing (ft):") +
  scale_linetype_manual(values = c(1, 2),
                        labels = c("Updated","Current")) +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5, margin = margin(45,0,20,0), size = 18),
        legend.position="top",
        legend.box.background = element_rect(colour = "black", size = 0.5),
        legend.box.margin = margin(0,0,0,0), legend.background = element_blank())

