2
votes

I am making a plot with data from an incomplete factorial design. Due to the design, I have different length for the manual scale for colour and the manual scale for fill. Thus, I get two legends. How could I delete one of them or even better combine them?

I have looked at those questions:

Merge separate size and fill legends in ggplot

How to merge color, line style and shape legends in ggplot

How to combine scales for colour and size into one legend?

However, the answers did not help me as they did not handle incomplete designs.

Here is some example data and the plot I produced so far:

#Example data 
Man1 <- c(25,25,30,30,30,30,35,35,40,40,40,40,45,45) 
Man2 <- c(25,25,30,30,40,40,35,35,40,40,30,30,45,45) 
DV <- c(24.8,25.2,29.9,30.3,35.2,35.7,34,35.1,40.3,39.8,35.8,35.9,44,44.8)
Data <- data.frame(Man1,Man2,DV)

#Plot 
ggplot(data = Data, aes(x = Man1, y = DV, group=as.factor(Man2), colour=as.factor(Man2))) +
  theme_bw()  +  
  geom_abline(intercept = 0, slope = 1, linetype = "longdash") +  
  geom_point(position = position_dodge(1)) 
  geom_smooth(method = "lm", aes(x = Man1, y = DV, group=as.factor(Man2),   fill=as.factor(Man2)))  + 
  scale_colour_manual(name = "Man2", values=c('grey20', 'blue','grey20','tomato3', 'grey20'))  + 
  scale_fill_manual(name = "Man2", values=c('blue','tomato3'))

This gives me the following picture:

ggplot of incomplete design with two legends

Could someone give me a hint how to delete one of the legends or even better combine them? I would appreciate it!

1

1 Answers

2
votes

By default the scale drops unused factor levels, which is relevant here because can only get lines for a couple of your groups.

You can use drop = FALSE to change this in the appropriate scale_*_manual() (which is for fill here).

Then use the same vector of colors for both the fill and color scales. I usually make a named vector for this.

# Make vector of colors
colors = c("25" = 'grey20', "30" = 'blue', "35" = 'grey20', "40" = 'tomato3', "45" = 'grey20')

#Plot 
ggplot(data = Data, aes(x = Man1, y = DV, group=as.factor(Man2), colour= as.factor(Man2))) +
    theme_bw()  +  
    geom_abline(intercept = 0, slope = 1, linetype = "longdash") +  
    geom_point(position = position_dodge(1))  +
geom_smooth(method = "lm", aes(fill=as.factor(Man2)))  + 
    scale_colour_manual(name = "Man2", values = colors)  + 
    scale_fill_manual(name = "Man2", values = colors, drop = FALSE)

enter image description here Alternatively, use guide = "none" to remove the fill legend all together.

ggplot(data = Data, aes(x = Man1, y = DV, group=as.factor(Man2), colour= as.factor(Man2))) +
    theme_bw()  +  
    geom_abline(intercept = 0, slope = 1, linetype = "longdash") +  
    geom_point(position = position_dodge(1))  +
    geom_smooth(method = "lm", aes(fill=as.factor(Man2)))  + 
    scale_colour_manual(name = "Man2", values = colors)  + 
    scale_fill_manual(name = "Man2", values=c('blue','tomato3'), guide = "none")

enter image description here