2

I am trying to merge the legends of a plot, but I can't figure out how. I looked up many examples, but in these examples people build the plots from one dataframe. My plot is build from four dataframes. I tried merging these dateframes with the merge and rbind function, but I can't figure out how to do it correctly.

Is it possible to merge the legends without merging the dataframes?

This is my code

fig_ObjPlur_ASSenTD <- ggplot(subset(gazetarget_ObjPlur_ASS)) + 
      geom_line(data=gazetarget_ObjPlur_ASS,aes(x=Time, y=Targetgaze, color="Target", linetype="ASS"), size=1) + 
      geom_line(data=gazeother_ObjPlur_ASS,aes(x=Time, y=Othergaze, color="Distractor",linetype="ASS"), size=1) + 
      geom_line(data=gazetarget_ObjPlur_TD,aes(x=Time, y=Targetgaze, color="Target", linetype="TD"), size=1) +
      geom_line(data=gazeother_ObjPlur_TD,aes(x=Time, y=Othergaze, color="Distractor", linetype="TD"), size=1) +

    scale_color_manual(name = "Condition & Group", values=c("blue","red")) +
    scale_linetype_manual(name = "Condition & Group", values=c(1,2)) +
    theme(legend.key.width=unit(2.5,"line"))

fig_ObjPlur_ASSenTD + ggtitle("Looks to target vs. distractor picture") +
      xlab("Time in milliseconds from sentence start") + 
      ylab("Proportion of looks") +  
      xlim(0,3000)

Which gives me this graph. Instead of two legends, I want one legend with 4 points:
- Target ASD (blue solid line)
- Distractor ASD (red solid line)
- Target TD (blue dashed line)
- Distractor TD (red dashed line)

Picture of graph

My dataframes look like this:

Example dataframe ObjPlur_ASS

Help would be appreciated!

Iris
  • 23
  • 3
  • Can you post sample data? Please edit **the question** with the output of `dput(df)` where `df` can be any of the two dataframes. Or, if it is too big with the output of `dput(head(df, 20))`. See also if these posts can inspire you: [1](https://stackoverflow.com/a/56824979/8245406), [2](https://stackoverflow.com/a/60028927/8245406). – Rui Barradas Feb 19 '20 at 11:48

1 Answers1

1

You have data in the wide format, it's easier to have legends with them in long format, so let's say your data is like this:

gazetarget_ObjPlur_ASS = data.frame(
  Time = seq(0,3000,length.out=10),
  Targetgaze = runif(10),
  Othergaze = runif(10)
)

gazetarget_ObjPlur_TD = data.frame(
  Time = seq(0,3000,length.out=10),
  Targetgaze = runif(10),
  Othergaze = runif(10)
)

We first add another column that annotates the dataset:

gazetarget_ObjPlur_ASS$Type = "ASD"
gazetarget_ObjPlur_TD$Type = "TD"

Then we set the colors / linetype for the legend:

COLS = c("blue","red","blue","red")
LINES = c("solid","solid","dashed","dashed")
names(COLS) = c("Target ASD","Distractor ASD","Target TD","Distractor TD")
names(LINES) = c("Target ASD","Distractor ASD","Target TD","Distractor TD")

Now we cast the data frame into long format and also change the values to Target / Distractor:

DF = rbind(gazetarget_ObjPlur_ASS,gazetarget_ObjPlur_TD) %>%
pivot_longer(-c(Time,Type)) %>% 
mutate(name=recode_factor(name,Targetgaze="Target",Othergaze="Distractor")) %>%
mutate(Group=paste(name,Type)) 

Then plot:

ggplot(DF,aes(x=Time,y=value,col=Group,linetype=Group)) + 
geom_line() +
scale_color_manual(values=COLS,breaks=names(COLS)) +
scale_linetype_manual(values=LINES,breaks=names(COLS))

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72