Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom tick marks on R plot

Tags:

plot

r

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

like image 844
Rubens Avatar asked Dec 02 '12 14:12

Rubens


People also ask

How do I add a tick to a graph in R?

To create a plot with tick marks manually between X-axis values in base R, we first need to create the plot without X-axis labels then add the axis values using axis function with appropriate labels, this will create tick marks as well as labels.

How do I add a minor tick in ggplot2?

Adding minor ticks to graphs is very simple. There are two mains ways, using the continuous scale functions such as scale_x_continuous() or using the guides() function, both from ggplot2 . Note that guide_prism_minor() does not work with discrete axes as they do not have minor breaks.

How do I customize axis labels in R?

To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.

How do I add Xticks?

Create x and y points using numpy. Plot x and y points over the plot, where x ticks could be from 1 to 10 (100 data points) on the curve. To add extra ticks, use xticks() method and increase the range of ticks to 1 to 20 from 1 to 10. To display the figure, use the show() method.


1 Answers

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

like image 140
Rubens Avatar answered Sep 16 '22 23:09

Rubens