0

I created a plot with two lines without any problem. The creation of the legend also worked without any problems. I just need to modify one line of the legend as it is dotted in the plot:

legend("bottom", legend = c("y", "y2", "A"), col = c("red", "orange", "blue"), 
  lwd=1, cex = 0.3)

so the line for A is dotted, how can I code this into R?

MrFlick
  • 178,638
  • 15
  • 253
  • 268
Joe94
  • 61
  • 5
  • 1
    What property did you set to make the dotted line? You'll probably just need to pass the appropriate values to the `lty=` parameter to the legend. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Dec 15 '20 at 23:48

1 Answers1

0

You can specify lty= , in the legend function, just that it's better to specify it before plotting, for example:

set.seed(111)
df = data.frame(x = 1:10, y =runif(10),y2=runif(10)+1,A=runif(10)-1)

col = c("red", "orange", "blue")
names(col) = c("y","y2","A")

linetypes = c(1,4,8)
names(linetypes) = c("y","y2","A")

plot(NULL,xlim=c(1,10),ylim=c(-2,2))

for(i in c("y","y2","A")){
    lines(df$x,df[,i],col=col[i],lty=linetypes[i])
}

legend("bottom", legend = c("y", "y2", "A"), 
col = c("red", "orange", "blue"), lty = linetypes,
lwd=1)

enter image description here

StupidWolf
  • 41,371
  • 17
  • 31
  • 62