One variable with three colors, but only one color on the legend

36 Views Asked by At

I have two variables in my table. I want one of them to appear with three colors on the graph, but only one color on the legend. How can I remove the NAs, which don't correspond to separate variables, from the legend?

My code below allows me to have the three colors (black, red and green) on the graph as well as the blue of the second variable. I want the black and blue colors only on the legend. Could you please help me?

Here are some observations of my database and my R script:

structure(list(Company = c("Ryanair", "Ryanair", "Ryanair", "Ryanair", 
"Ryanair", "Ryanair", "Ryanair", "Ryanair", "Ryanair", "Ryanair", 
"Singapore Airlines", "Singapore Airlines", "Singapore Airlines", 
"Singapore Airlines", "Singapore Airlines", "Singapore Airlines", 
"Singapore Airlines", "Singapore Airlines", "Singapore Airlines", 
"Singapore Airlines"), Year = c(2012, 2013, 2014, 2015, 2016, 
2017, 2018, 2019, 2020, 2021, 2012, 2013, 2014, 2015, 2016, 2017, 
2018, 2019, 2020, 2021), tei_tl = c(0.79265, 0.8301, 0.90591, 
0.89794, 0.92407, 0.91998, 1, 0.91571, 0.80457, 0.69001, 0.80559, 
0.80162, 0.79714, 0.76219, 0.75193, 0.81753, 0.83193, 0.79027, 
0.28713, 0.6495), EU = c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0), Ann_av_te = c(0.77291, 0.80389, 0.83033, 
0.81141, 0.79951, 0.8164, 0.83807, 0.81942, 0.56056, 0.66667, 
0.77291, 0.80389, 0.83033, 0.81141, 0.79951, 0.8164, 0.83807, 
0.81942, 0.56056, 0.66667)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))
ggplot(data = datateff) + 
  geom_line(aes(x = Year, y = tei_tl, group=Company, color=(ifelse((EU == 1), "green", 'black')))) + 
  labs(x="Year", y="") + 
  geom_point(size=1, aes(x = Year, y = tei_tl, size=.7, group=Company, color=tei_tl <1)) + 
  geom_line(aes(x = Year, y = Ann_av_te, group=Company, color=('#33CCFF'))) + 
  scale_color_manual(values = c( '#33CCFF',"black", "red", "green", "black"), 
                     name="", labels=c("TEI annual averages", "TEI")) + 
  facet_wrap(~ Company, nrow=5, ncol=4) + 
  theme(axis.text=element_text(size=6), axis.title=element_text(size=8), 
        text=element_text(size=8), 
        legend.text = element_text(colour="black", size = 6), 
        legend.title = element_text(colour="black", size=6), 
        legend.position = c(1.0, .01), 
        legend.justification = c("right", "bottom"), 
        legend.box.just = "right", 
        legend.margin = margin(6, 6, 6, 6))
1

There are 1 best solutions below

2
stefan On

If you want to show only some categories in the legend you could do so via the breaks= argument of scale_color_manual which however requires to use a named vector of colors.

Using a minimal reproducible example based on ggplot2::mpg:

library(ggplot2)

ggplot(mpg, aes(cty, hwy, color = class)) +
  geom_point() +
  scale_color_manual(
    values = setNames(
      scales::pal_brewer(type = "qual")(7),
      unique(mpg$class)
    ),
    breaks = c("compact", "midsize"),
    labels = c(compact = "Compact", midsize = "Midsize")
  )

UPDATE And based on your real data it's just a matter of properly naming categories:

ggplot(data = datateff) +
  geom_line(aes(
    x = Year, y = tei_tl, group = Company,
    color = ifelse(EU == 1, "non_tei", "tei")
  )) +
  labs(x = "Year", y = "") +
  geom_point(size = 1, aes(
    x = Year, y = tei_tl, size = .7, group = Company,
    color = tei_tl < 1
  )) +
  geom_line(aes(x = Year, y = Ann_av_te, group = Company, color = "ann_av_te")) +
  scale_color_manual(
    values = c(
      ann_av_te = "#33CCFF", tei = "black", "FALSE" = "red",
      non_tei = "green", "TRUE" = "black"
    ),
    name = NULL,
    breaks = c("ann_av_te", "tei"),
    labels = c(ann_av_te = "TEI annual averages", tei = "TEI")
  ) +
  facet_wrap(~Company, nrow = 5, ncol = 4) +
  theme(
    axis.text = element_text(size = 6), axis.title = element_text(size = 8),
    text = element_text(size = 8),
    legend.text = element_text(colour = "black", size = 6),
    legend.title = element_text(colour = "black", size = 6),
    legend.position = c(1.0, .01),
    legend.justification = c("right", "bottom"),
    legend.box.just = "right",
    legend.margin = margin(6, 6, 6, 6)
  )

enter image description here