DatamineR
DatamineR

Reputation: 9618

The option `lty` in `plot()` not working

I am trying to generate a plot as interrupted line. But to my surprise the option lty=2 in the function plot() doesnt work:

plot(1:10,lty=2) 

Can someone help me?

Upvotes: 2

Views: 4329

Answers (2)

Gavin Simpson
Gavin Simpson

Reputation: 174788

Why would it? You are drawing points. lty only affects the drawing of lines. It does work if you do, for example;

plot(1:10, type = "b", lty = 2)

which gives

enter image description here

I chose type = "b" here to illustrate the difference; it means both lines and points. Notice how lty affects the line parts, but not the points themselves. If you look at ?plot.default you'll see

 plot(x, y = NULL, type = "p",  xlim = NULL, ylim = NULL,
      log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
      ann = par("ann"), axes = TRUE, frame.plot = axes,
      panel.first = NULL, panel.last = NULL, asp = NA, ...)

which indicates the type = "p", for points, is the default if not specified by the user.

The points are drawn using a font character or glyph, not a line wrapped into a circle. These characters for the points are like any other text character on the plot (the axis and tick labels for example). As they are not drawn using lines lty does not affect them.

symbols() is an alternative function that does draw points using lines. For example

plot(1:10, type = "n")
symbols(1:10, 1:10, circles = rep(0.1, 10), lty = 2, inches = FALSE, add = TRUE)

which produces

enter image description here

You don't need the separate plot() call, just leave out the add = TRUE part of the call to symbols(), but if you do that, it gives different axes limits on the plot compared to the one you got with plot() above.

symbols(1:10, 1:10, circles = rep(0.1, 10), lty = 2, inches = FALSE)

Upvotes: 7

James
James

Reputation: 66834

You need to specify that you want to plot lines with type="l". By default plot will use points.

plot(1:10,lty=2,type="l")

Upvotes: 3

Related Questions