skylinedreamer
skylinedreamer

Reputation: 71

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

Upvotes: 1

Views: 295

Answers (3)

Jota
Jota

Reputation: 17621

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")

Upvotes: 2

C_Z_
C_Z_

Reputation: 7816

plot alone cannot actually do what you want. You need to call plot and then segments.

XMIN = 4
XMAX = 5

plot(x, y, type="b", ann = FALSE, axes = FALSE, pch = 20)
xinds = x>=XMIN & x<=XMAX
segments(x[xinds][1:sum(xinds)-1],y[xinds][1:sum(xinds)-1],
         x[xinds][2:sum(xinds)],y[xinds][2:sum(xinds)], lwd=3)

Upvotes: 0

IRTFM
IRTFM

Reputation: 263451

Segments takes (x0,y0) and draws to (x1,y1)

segments(x[4],y[4],x[5],y[5], lwd=3)

Upvotes: 1

Related Questions