Search code examples
rggplot2colorslegendline-plot

R ggplot2 need custom legend to have less values than corresponding line plot values to avoid duplicates


I have data which looks like the following:

library(tidyverse)
library(RColorBrewer)

dat<-tibble(
  group =  c(rep("Group 1", 10), rep("Group 2", 10), rep("Group 3", 10)),
  iter = rep(c(rep(1, 5), rep(2, 5)), 3),
  group_iter = paste(group, iter, sep = "_"),
  x = 1:30,
  y = 11:40
)
 # A tibble: 30 × 5
   group    iter group_iter     x     y
   <chr>   <dbl> <chr>      <int> <int>
 1 Group 1     1 Group 1_1      1    11
 2 Group 1     1 Group 1_1      2    12
 3 Group 1     1 Group 1_1      3    13
 4 Group 1     1 Group 1_1      4    14
 5 Group 1     1 Group 1_1      5    15
 6 Group 1     2 Group 1_2      6    16
 7 Group 1     2 Group 1_2      7    17
 8 Group 1     2 Group 1_2      8    18
 9 Group 1     2 Group 1_2      9    19
10 Group 1     2 Group 1_2     10    20
# ℹ 20 more rows
# ℹ Use `print(n = ...)` to see more row

Where values in group are repeated by iteration and produce corresponding x and y outcomes. I am attempting to use ggplot2 to produce the following line graph:

groups<-dat%>%distinct(group_iter, .keep_all = T)%>%pull(group)
uniqe_groups<-length(groups)
color_vals<-brewer.pal(uniqe_groups, name = "Set1")
color_vals1<-tibble(group = groups)%>%
  left_join(
    tibble(group = unique(dat$group), color = color_vals),
    by = "group"
  )%>%pull(color)

dat%>%ggplot(aes(x=x,y=y, color = group_iter, fill = group_iter)) + geom_line() +
  scale_color_manual("", labels = groups, values = color_vals1)

Plot output

The catch, however, is that I need the legend to not have duplicated Group values. So for example, the legend in the previous plot would look like this:

desired legend output

The problem I am having is I can't seem to find a good way to customize the legend like this (i.e., less legend values using scale_color_manual than color/fill in the plot) without either not having the correct number of lines or producing an error:

error in `palette()`: ! insufficient values in manual scale

I have search fairly throughly on StackOverlow and other sources:

ggplot2 Error: Insufficient values in manual scale

Fixing Error: Insufficient values in manual scale. 2 needed but only 1 provided. In R

How to add manual colors for a ggplot2 (geom_smooth/geom_line)

ggplot2 custom legend shapes

but none of them seem to solve this specifc problem. Help desperately needed please and thank you!


Solution

  • This will achieve what you want:

    library(tibble)
    library(RColorBrewer)
    library(ggplot2)
    
    dat <- tibble(
      group =  c(rep("Group 1", 10), rep("Group 2", 10), rep("Group 3", 10)),
      iter = rep(c(rep(1, 5), rep(2, 5)), 3),
      group_iter = paste(group, iter, sep = "_"),
      x = 1:30,
      y = 11:40
    )
    
    groups <- unique(dat$group)
    
    color_vals <- brewer.pal(length(groups), name = "Set1")
    
    dat %>%
      ggplot(aes(x = x, y = y, color = group, group = group_iter)) + 
      geom_line() +
      scale_color_manual("", labels = groups, values = color_vals)
    

    1