Search code examples
rplotlines

How to use lwd for specific points


I would like to change the thickness of the line between two points [(4,1) and (5,0)] in R Plot.

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
y <- c(0, 1, 0, 1, 0, 1, 0, 1, 0)

plot(x, y, type="b", ann = FALSE, axes = FALSE, pch = 20, lwd=ifelse(x>=4 & x<=5, 3, 1))

But, I am only able to make the points thicker. I also need to make the line thicker. Could you guys advice me on where I am wrong.

Plot

I tried with lines and segments. I get a line that is touching both the points. But, I need the thicker line to be of the same length as other lines. enter image description here

Solved using lines enter image description here


Solution

  • With plot, lwd can't accept a vector. You might want to try using lines instead

    plot(x, y, type="b", ann = FALSE, axes = FALSE, pch = 20)
    
    lines(x[4:5], y[4:5], lwd = 3, type = "b")