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
  • 195,160
  • 17
  • 277
  • 295
Joe94
  • 91
  • 7
  • 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
  • 45,075
  • 17
  • 40
  • 72