0

Simple question: How can I assign custom names AND styles for linetypes of different plots at the same time and print legends.

All plots use library(ggplot2).

Example 1: I am able to assign my preferred linetype and plot the legend according to color.

ggplot() + 
     geom_line(data=data.frame(x=c(0, xv[1]), y=c(0, 1)), aes(x=x, y=y, color = "this one should be dashed"), linetype = "dashed")+
     geom_line(data=data.frame(x=c(0, xv[2]), y=c(0, 1)), aes(x=x, y=y, color = "this one should be twodash"), linetype = "twodash")+
     geom_line(data=data.frame(x=c(0, xv[3]), y=c(0, 1)), aes(x=x, y=y, color = "this one should be dotted"), linetype = "dotted")+ 
     guides(color = guide_legend("by color"))

enter image description here

Example 2: I am able to assign my preferred linetype but when I try to plot the legend according to linetype no legend shows up.

ggplot() + 
     geom_line(data=data.frame(x=c(0, 10), y=c(0, 1)), aes(x=x, y=y, linetype = "this one should be dashed"), linetype = "dashed")+
     geom_line(data=data.frame(x=c(0, 20), y=c(0, 1)), aes(x=x, y=y, linetype = "this one should be twodash"), linetype = "twodash")+
     geom_line(data=data.frame(x=c(0, 30), y=c(0, 1)), aes(x=x, y=y, linetype = "this one should be dotted"), linetype = "dotted")+ 
     guides(linetype = guide_legend("by linetype"))

enter image description here

Example 3: If I use the default linetype without specifying my preferred types I can plot the legend by linetype just fine

ggplot() + 
     geom_line(data=data.frame(x=c(0, 10), y=c(0, 1)), aes(x=x, y=y, linetype = "this one should be dashed"))+
     geom_line(data=data.frame(x=c(0, 20), y=c(0, 1)), aes(x=x, y=y, linetype = "this one should be twodash"))+
     geom_line(data=data.frame(x=c(0, 30), y=c(0, 1)), aes(x=x, y=y, linetype = "this one should be dotted"))+ 
     guides(linetype = guide_legend("by linetype"))

enter image description here

Mario Niepel
  • 1,095
  • 4
  • 19
  • 1
    Take a look at `scale_linetype_manual()`. If you want a legend, things need to be inside the `aes()`. You use the `scale_*` functions to assign styling to your different levels. – MrFlick Dec 04 '20 at 00:40
  • 2
    Also it's usually recommended that you put your data in long format so it's easier to work with `ggplot`. See these examples https://stackoverflow.com/a/62924999/786542 & https://stackoverflow.com/a/59445501/786542 & https://stackoverflow.com/a/50240153/786542 – Tung Dec 04 '20 at 00:45
  • Understood about the long format data frames. This is a toy problem that leads up to a different problem I have. I wanted to sort this one out first but keep the structure as similar to the second one. – Mario Niepel Dec 04 '20 at 01:06

1 Answers1

1

Thank you @MrFlick. I was obviously doing it wrong and need to use scale_linetype_manual() to assign a custom order for the linetypes.

ggplot() + 
     geom_line(data=data.frame(x=c(0, 10), y=c(0, 1)), aes(x=x, y=y, color = "same group", linetype = "A is dashed")) +
     geom_line(data=data.frame(x=c(0, 20), y=c(0, 1)), aes(x=x, y=y, color = "same group", linetype = "B is twodash")) +
     geom_line(data=data.frame(x=c(0, 30), y=c(0, 1)), aes(x=x, y=y, color = "another group", linetype = "C is solid")) + 
     scale_linetype_manual(values=c("dashed","twodash","solid")) + 
     guides(linetype = guide_legend("by linetype")) +
     guides(color = guide_legend("by color"))

enter image description here

Mario Niepel
  • 1,095
  • 4
  • 19