4

In R I'm trying to generate a plot where I want to apply unique colors, line types, transparencies, and line thicknesses by case grouping. As currently implemented two legend plots are generated instead of one. The second legend plot is the only one that I can change the legend title. Presumably I've made a mistake any help would be greatly appreciated.

Ultimately I want to generate a single legend and have the style changes and labeling changes take effect.

library(ggplot2)
temp_df <- data.frame(year = integer(50), value = numeric(50), case = character(50))
temp_df$year <- 1:50
temp_df$value <- runif(50)
temp_df$case <- "A"
df <- temp_df
temp_df$value <- runif(50)
temp_df$case <- "B"
df <- rbind(df, temp_df)
LineTypes <- c("solid", "dotted")
colors <- c("red", "black")
linealphas <- c(1, .8)
linesizes <- c(1, 2)
Plot <- ggplot(df, aes(x = year, y = value, group = case))+
  geom_line(aes(linetype = case, color = case, size = case, alpha = case))+
  scale_linetype_manual(values = LineTypes)+
  scale_color_manual(values = colors)+
  scale_y_continuous(limits = c(0, 1), labels = scales::percent)+
  scale_alpha_manual(values = linealphas)+
  scale_size_manual(values = linesizes)+
  xlab("Year")+
  ylab("Percentage%")+
  labs(color = "Scenario")+
  theme_minimal()
Plot

Result of Current Code

1
  • You can use the name = argument inside your manual scale_* functions to change the legend title, and e.g. + guides(linetype = FALSE) to hide the legend associated with a scale_* function. But personally, I would use only colour to distinguish groups in this case.
    – neilfws
    May 8, 2018 at 23:27

3 Answers 3

3

If you want ggplot to merge the legends then they all have to have the same title. You can specify the legend title with the name argument in the scales:

ggplot(df, aes(x = year, y = value, group = case))+
  geom_line(aes(linetype = case, color = case, size = case, alpha = case)) +
  scale_linetype_manual(values = LineTypes, name = "Scenario")+
  scale_color_manual(values = colors, name = "Scenario")+
  scale_y_continuous(limits = c(0, 1), labels = scales::percent)+
  scale_alpha_manual(values = linealphas, name = "Scenario")+
  scale_size_manual(values = linesizes, name = "Scenario")+
  xlab("Year")+
  ylab("Percentage%")+
  theme_minimal()

enter image description here

2

A coworker pointed out a resolution to me the key was to remove the guides so that only one of styles that I had defined was being used for the legend.

     guides(size = FALSE)+
     guides(alpha = FALSE)+
     guides(linetype = FALSE)+ 

His explanation for this was that R doesn't recognize that the vector of factors defining the properties of the plot are necessarily related. As a result it will generate multiple legends when only one is desired.

    library(ggplot2)
    temp_df<-data.frame(year=integer(50),value=numeric(50),case=character(50))
    temp_df$year<-1:50
    temp_df$value<-runif(50)
    temp_df$case<-"A"
    df<-temp_df
    temp_df$value<-runif(50)
    temp_df$case<-"B"
    df<-rbind(df,temp_df)
    LineTypes<-c("solid","dotted")
    colors<-c("red","black")
    linealphas<-c(1,.8)
    linesizes<-c(1,2)
    Plot<-ggplot(df,aes(x=year,y=value,group=case))+
      geom_line(aes(linetype=case, color=case, size=case, alpha =case))+
      scale_linetype_manual(values=LineTypes)+
      scale_color_manual(values=colors)+
      scale_y_continuous(limits=c(0,1),labels = scales::percent)+
      scale_alpha_manual(values=linealphas)+
      scale_size_manual(values=linesizes)+
      xlab("Year")+
      ylab("Percentage%")+
      labs(color = "Scenario")+
      guides(size = FALSE)+
      guides(alpha = FALSE)+
      guides(linetype = FALSE)+
      theme_minimal()
    Plot

enter image description here

1
  • 1
    But then the legend key (linetype) for scenario B is not totally correct, is it? See this answer for the fix
    – Tung
    May 8, 2018 at 23:38
0

Can't you just remove the line "labs(color = "Scenario")"? This is the plot that gets generated. Not sure if it's missing anything that you need.

The result seems fine to me: (seems fine to me)

2
  • Two things, I want to change the title of the legend. Yes technically I could just give the column in question a different name but I'd prefer to do it in the plotting step. Also the style changes in the plot aren't propagating to both legends.
    – vcelloho
    May 8, 2018 at 23:14
  • Certainly, it is a work around. However, I don't know why we can not modify the legend title in this example. I tried without success
    – lcgodoy
    May 8, 2018 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.