Dekike
Dekike

Reputation: 1294

How to add a second legend using different `geom_line`?

I am plotting the relationship between two variables (X and Y) for different individuals (IDs). This relationship is shown both with the real values (geom_point) and with lines which represent the prediction of the relationship between the variables for different individuals Linear Mixed Effect Models (LME). On top of that, the linear relationship between the two variables and for the different individuals is done using three levels of a second quantitative predictor (Z).

Thus, what I do is to use geom_point() for showing the relationship between raw values of X and Y. Then, I use three geom_line() for three LME with different levels of Z. Thus, each geom_line() draws the six lines for the six IDs for a fixed Z. So, since I have 3 Z levels and I have 3 geom_line(), I have 18 lines.

I tried this (note: code is simplified):

Plot_legend <- ggplot(df, aes(x=X, y=Y, colour=ID)) + 
  geom_point(size=1.5,alpha=0.2) +
  geom_line(aes(y=predict(model,df.Z_low), group=ID, linetype = c("1")), size=1.5, alpha=0.6, color = line_colors[3]) +
  geom_line(aes(y=predict(model,df.Z_medium), group=ID, linetype = c("2")), size=1.5, alpha=0.6, color = line_colors[2]) +
  geom_line(aes(y=predict(model,df.Z_high), group=ID, linetype = c("3")), size=1.5, alpha=0.6, color = line_colors[1]) +
  geom_abline(aes(slope=1,intercept=0),linetype="dashed",color="grey52",size=1.5) +
  theme_bw() + 
  theme(legend.text=element_text(size=18),
        legend.title = element_text(size=19, face = "bold",hjust = 0.5),
        legend.key=element_blank(),
        legend.background = element_rect(colour = 'black', fill = 'white', size = 1, linetype='solid')) +
  guides(color=guide_legend(override.aes=list(fill=NA)))

enter image description here

However, as you can see, the legend for the three geom_line() is not what I desire. I would like to appear as title Z instead of c("10th"). Also, the colours of the legend for the three geom_line() do not correspond with the true colours for the different geom_line(), and some lines are dashed.

Does anyone know how to solve this?

Plot using Duck's advice

enter image description here

Upvotes: 1

Views: 197

Answers (2)

Dekike
Dekike

Reputation: 1294

I used next code finally:

Plot_legend <- ggplot(df, aes(x=X, y=Y, colour=ID)) + 
  geom_point(size=1.5,alpha=0.2) +
  geom_abline(aes(slope=1,intercept=0),linetype="dashed",color="grey52",size=1.5) +
  theme_bw() + 
  theme(legend.text=element_text(size=18),
        legend.title = element_text(size=19, face = "bold",hjust = 0.5),
        legend.key=element_blank(),
        legend.background = element_rect(colour = 'black', fill = 'white', size = 1, linetype='solid')) +
  guides(color=guide_legend(override.aes=list(fill=NA)))

Plot_legend

Plot_legend_2 <- Plot_legend +
  geom_line(aes(y=predict(model,df.Z_low), group=ID, linetype = "m1"), size=1.5, alpha=0.6, color = line_colors[3]) +
  geom_line(aes(y=predict(model,df.Z_medium), group=ID, linetype ="m2"), size=1.5, alpha=0.6, color = line_colors[2]) +
  geom_line(aes(y=predict(model,df.Z_high), group=ID, linetype ="m3"), size=1.5, alpha=0.6, color = line_colors[1]) +
  scale_linetype_manual(values = c(m1 = "solid", m2 = "solid", m3 = "solid"),labels = c(m1 = "1", m2 = "2", m3 = "3")) +
  labs(color = "ID", linetype = expression(Z)) +
  guides(linetype = guide_legend(override.aes = list(color = line_colors)))

Plot_legend_2

enter image description here

Upvotes: 1

Duck
Duck

Reputation: 39613

Try this approach. As no data was shared I can test it but it can address in right path:

library(ggplot2)
#Code
Plot_legend <- ggplot(df, aes(x=X, y=Y, colour=ID)) + 
  geom_point(size=1.5,alpha=0.2) +
  geom_line(aes(y=predict(model,df.Z_low), group=ID, linetype = c("1")),
            size=1.5, alpha=0.6, color = line_colors[3]) +
  geom_line(aes(y=predict(model,df.Z_medium), group=ID, linetype = c("2")),
            size=1.5, alpha=0.6, color = line_colors[2]) +
  geom_line(aes(y=predict(model,df.Z_high), group=ID, linetype = c("3")),
            size=1.5, alpha=0.6, color = line_colors[1]) +
  geom_abline(aes(slope=1,intercept=0),linetype="dashed",color="grey52",size=1.5) +
  theme_bw() +
  scale_linetype_manual(values=c('solid','solid','solid'))+
  scale_color_manual(values=c(line_colors[3],line_colors[2],line_colors[1]))+
  labs(linetype='Z')
  theme(legend.text=element_text(size=18),
        legend.title = element_text(size=19, face = "bold",hjust = 0.5),
        legend.key=element_blank(),
        legend.background = element_rect(colour = 'black', fill = 'white', size = 1, linetype='solid')) +
  guides(color=guide_legend(override.aes=list(fill=NA)))

Upvotes: 1

Related Questions