18

I'm plotting a cdf of some data, and I've added logarithmic scale on the "x" axis. The ticks spacing is exactly as I want it to be, but I'd like to be able to add some tick marks on specific points.

I don't want to change the distribution of the ticks in my plot, from n by n to m by m, I want simply to have, among the ticks from n by n, some further tick marks on some values.

I'd like to have it reflected in both x and y axis, so that I can fit a grid into these new marks throughout the graph.

So far I have the graph, and the grid -- I don't mind about having the grid behind or upon the graph, I just want to add some custom ticks.

# Cumulative Distribuition
pdf("g1_3.pdf")

plot(x = f$V2, y = cumsum(f$V1), log = "x", pch = 3,
     xlab = "Frequency", ylab = "P(X <= x)",
     panel.first = grid(equilogs = FALSE))

axis(1, at = c(40, 150))
abline(h = 0.6, v = 40, col = "lightgray", lty = 3)
abline(h = 0.6, v = 150, col = "lightgray", lty = 3)

dev.off()

UPDATE: The graph I have so far:

enter image description here

tonytonov
  • 25,060
  • 16
  • 82
  • 98
Rubens
  • 14,478
  • 11
  • 63
  • 92
  • You can try to add extra ticks with function axis() after plot() function, for example, axis(1,at=c(3,25,345)) – Didzis Elferts Dec 02 '12 at 14:16
  • Thanks, I've tried this but it only adds a tick mark, the grid is not being reconfigured. What are the three columns for? I've noticed 345 is the place where I want to sit the tick mark, but what about 3 and 25? – Rubens Dec 02 '12 at 14:28
  • Please read `?axis` and `?grid`. – Roland Dec 02 '12 at 14:42
  • Numbers 3 and 25 are just example. Another solution would be to use function abline() and make horizontal and vertical lines where you need – Didzis Elferts Dec 02 '12 at 14:42
  • @Didzis I do understand 3 and 25 were just an example, what I didn't get is what they are for, what do they represent in the plot. Anyway, I'll try abline(). – Rubens Dec 02 '12 at 15:13
  • 2
    When in doubt, *try an experiment*; if you ran the code @Didzis provided (before running `dev.off()`), you would see that it would add ticks to the bottom axis (the first, or `side`, argument, is equal to 1) at x-locations 3, 25, *and* 345 ... – Ben Bolker Dec 02 '12 at 17:50
  • Ah, thanks, I've noticed it now, and I've managed to add the lines. Happens though it's some what confusing now, and I'd like to take off two specific lines -- the one connected to frequency = 50 and frequency = 100. Any tip? Regards! (I've updated the plot). – Rubens Dec 02 '12 at 18:49
  • if you want to *remove* ticks, you actually need to suppress the axes entirely and then add the ones you want, i.e. `plot(...,axes=FALSE); axis(side=1,ticks=c([all the ticks you want])); axis(side=2); box()` (the second `axis` command specifies a y axis with default ticks) – Ben Bolker Dec 02 '12 at 19:31

1 Answers1

19

Considering the initial script, and the tips given by @BenBolker, I had to use:

axis(side = 1, at = c([all the ticks you want]))

in order to add the ticks in the graph. Here's the final result:

# Cumulative Distribuition
pdf("g1_3.pdf")

plot(x = f$V2, y = cumsum(f$V1), log = "x", pch = 3,
     xlab = "Frequency", ylab = "P(X <= x)", axes = FALSE)

ticks = c(1, 5, 10, 40, 150, 500, 1000)
axis(side = 1, at = ticks)
axis(side = 2)

abline(h = seq(0, 1, 0.2), v = ticks, col = "lightgray", lty = 3)
box()

Final result to the graph I was trying to generate

tonytonov
  • 25,060
  • 16
  • 82
  • 98
Rubens
  • 14,478
  • 11
  • 63
  • 92
  • 1
    nice answer (you don't need semicolons at the end of the lines, by the way) – Ben Bolker Dec 24 '12 at 04:29
  • @BenBolker Thanks for the support, I was struggling to get the idea of using lists with different lengths in the parameters `h` and `v` of `abline` call, but it seems the lines are generated separately. – Rubens Dec 24 '12 at 04:34