1

I want to plot 180 points on the x-axis with the corrisponding y-values. But 180 points on the x-axis makes the plot/screen way too crowded. So i want to tell R only to label every 10th (out of my x-vector) on the x-axis. Anyone has an idea how to do it?

Code example:

y <- c(101:280)
x <- c(1:180)
plot (x, y)

Now on the x-axis is every of the 180 points labeled. I want every point in the plot, but only every tenth as written on the x-axis. Hope you know what i am trying to say ;-)

r2evans
  • 141,215
  • 6
  • 77
  • 149
R-Newbie
  • 11
  • 1
  • 1
    that's only true if `x` is a factor, the default is `pretty(x)` which would mark 0, 50, 100, and 150. you can also use `xaxp`, see `?par` `plot(x, y, xaxp = c(range(x), 10))` – rawr Jul 24 '20 at 20:37

2 Answers2

0

enter image description hereAfter your plot call, you want to use axis.

plot(x,y)
axis(side = 1, at = x[c(rep(FALSE, 9), rep(TRUE, 1))])

Here, I am subsetting each of your x labels via a logical vector by taking 9 FALSE, then 1 TRUE, meaning every tenth.

salexir
  • 46
  • 1
0

First plot without an x-axis

plot(x, y, xaxt = "n")

then add an axis with a sequence defined from the original data

axis(1, at = seq(min(x), max(x), by = 10))

enter image description here

If the numbers/labels are too compact, then R will reduce some of them (as shown). You can control this many ways, including this (on a fresh plot):

axis(1, at = seq(min(x), max(x), by = 10), las = 2)

rotated labels

r2evans
  • 141,215
  • 6
  • 77
  • 149