Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add minor tick marks to an axis besides its main tick marks?

Tags:

plot

r

I have two questions.

  1. I was wondering how I can add two words, one appearing in place of "0", the other appearing in place of "10" on the X and Y axes while preserving the rest of the remaining numbers on the axes (i.e., 1 thru 9)?
  2. How can I add minor tick marks to both axes?

Here is my R code, with no success:

plot(0:10,0:10,type="n",axes=FALSE,xlab="JBL",ylab="PRAC")

axis(side=1,at=c("AL",1,2,3,4,5,6,7,8,9,"BL"),las=1)
axis(side=2,at=c("CL",1,2,3,4,5,6,7,8,9,"DL"),las=1)
box()
like image 323
rnorouzian Avatar asked Dec 04 '16 03:12

rnorouzian


People also ask

How do you add a tick mark to a graph?

Add tick marks on an axisClick the chart, and then click the Chart Design tab. Click Add Chart Element > Axes > More Axis Options. On the Format Axis pane, expand Tick Marks, and then click options for major and minor tick mark types.

How do I insert a minor tick in MatPlotLib?

Minor tick labels can be turned on by setting the minor formatter. MultipleLocator places ticks on multiples of some base. StrMethodFormatter uses a format string (e.g., '{x:d}' or '{x:1.2f}' or '{x:1.1f} cm' ) to format the tick labels (the variable in the format string must be 'x' ).

How do you add a minor tick in origins?

Select the Enable Minor Labels check box to display minor tick labels. Select the Minor Labels on Major Ticks check box to display both a minor tick label and a major tick label for each major tick.


2 Answers

For the first question, you should use labels not at. Read ?axis.

For the second, you can call axis or Axis twice on the same axis, but using rug is easier. It is a wrapper function for Axis. For example, you can do:

plot(0:10, 0:10, axes = FALSE)  ## `axes` is an argument to `plot.default`
axis(1, at = 0:10, labels = c("AL",1,2,3,4,5,6,7,8,9,"BL"))
rug(x = 0:9 + 0.5, ticksize = -0.01, side = 1)
axis(2, at = 0:10, labels = c("CL",1,2,3,4,5,6,7,8,9,"DL"))
rug(x = 0:9 + 0.5, ticksize = -0.01, side = 2)

enter image description here

You can see how rug works in its source code:

Axis(side = side, at = x, labels = FALSE, lwd = 0, lwd.ticks = lwd, 
    col.ticks = col, tck = ticksize, ...)

Read ?par for how tck works. The default tck for an axis is -0.02, so if you want a smaller one for "minor axis", set it to half. Here, the sign means direction. Positive values give ticks pointing inside the plot, while negative ones give ticks pointing outside the plot.

like image 61
Zheyuan Li Avatar answered Sep 19 '22 00:09

Zheyuan Li


For the minor ticks, you could use the minor.tick function from the hmisc package.

minor.tick(nx=2, ny=2, tick.ratio=0.5, x.args = list(), y.args = list())

https://www.rdocumentation.org/packages/Hmisc/versions/4.1-1/topics/minor.tick

like image 31
PrinzvonK Avatar answered Sep 22 '22 00:09

PrinzvonK