17
votes

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

1
You can try to add extra ticks with function axis() after plot() function, for example, axis(1,at=c(3,25,345))Didzis Elferts
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
Please read ?axis and ?grid.Roland
Numbers 3 and 25 are just example. Another solution would be to use function abline() and make horizontal and vertical lines where you needDidzis Elferts
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

1 Answers

19
votes

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