rlegendi
rlegendi

Reputation: 10606

How to display only every Nth element on a plot in R?

Let's say I would like to display the results of a sequence:

plot(runif(10,0,1), type="o")

in a way to display all of the elements with a connected line but plot the corresponding icons (circles/diamonds/etc.) assigned with pch only for every second, third, or Nth element?

Should I create two sequence, one with filtered elements and display it as a different line()? Isn't there a bit elegant way to do that?

Thanks in advance!

Upvotes: 2

Views: 2361

Answers (2)

Julius Vainora
Julius Vainora

Reputation: 48241

In your case probably that would be

plot(runif(10, 0, 1), type = "o", pch = c(20, rep(NA, 3)))

for lets say every 4th element.

Upvotes: 6

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193647

If I understand your question correctly, you can just add the pch argument to plot with whatever sequence you are looking for: For example:

set.seed(1)
# I've used 'type="b"' just for clarity
plot(runif(10, 0, 1), 
     type="b", 
     pch=rep(c(1, 2, 3), length.out=10))

will give you this:

enter image description here

Upvotes: 1

Related Questions