# magicaxis

The goal of magicaxis is provide functions to make pretty axes (major and minor) on scientific plots. Particularly effort is made on producing nice log plot outputs. The core function produces pretty axis labelling in a number of circumstances that are often used in scientific plotting. There is a higher level interface to a generic plot function that will usually produce nice plots, even without much though on the users part.

## Installation

You can install the released version of magicaxis from CRAN with:

``install.packages("magicaxis")``

And the development version from GitHub with:

``````# install.packages("devtools")
devtools::install_github("asgr/magicaxis")``````

## Example

An example of using magplot:

``````library(magicaxis)
## basic example code
x=10^{1:9}
y=1:9
magplot(log10(x),y,unlog='x')``````

``magplot(x,y,log='x')``

``````
#Not ideal to have two decades between major labels:

magplot(x,y,log='x',majorn=c(10,5))``````

``magplot(x,y,log='xy',majorn=c(10,5,5,5),side=1:4)``

``````
#Sometimes it is helpful to focus on where most of the data actually is.
#Using a single value for xlim and ylim sigma clips the data to that range.
#Here a value of 2 means we only show the inner 2-sigma (2% to 98%) range.
#The 'auto' option allows magclip to dynamically estimate a clip value.

temp=cbind(rt(1e3,1.5),rt(1e3,1.5))
magplot(temp)``````

``magplot(temp, xlim=2, ylim=2)``

``magplot(temp, xlim='auto', ylim='auto')``

``````
#Some astronomy related examples (and how to display the solar symbol):

temp=cbind(runif(10,8,12),runif(10,0,5))

magplot(temp[,1:2], xlab=expression(M['\u0298']), ylab=expression(M['\u0298']/Yr), unlog='xy')``````

``````
#With z scaling

z=sqrt(9:1)
magplot(x, y, z, log='x', position='topleft')``````

An example of using maghist:

``````maghist(rnorm(1e4))
#> [1] "Summary of used sample:"
#>      Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
#> -3.347802 -0.680214  0.002396  0.006846  0.682562  3.731962
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 1.004617 1.010325 1.006027 2.016355
#> [1] "Using 10000 out of 10000"``````

``````maghist(rnorm(1e4), xlim=c(-2,4))
#> [1] "Summary of used sample:"
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
#> -1.99729 -0.61235  0.03331  0.06389  0.69167  3.59493
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 0.9504591 0.9671921 0.9747294 1.8847496
#> [1] "Using 9763 out of 10000 (97.63%) data points (237 < xlo & 0 > xhi)"``````

``````
#Notice the x-limits are close to -3/3, since  if we ask for xlim=3 (a 3-sigma range)

maghist(rnorm(1e4), xlim=3, verbose = FALSE)``````

``````
#The 'auto' option allows magclip to dynamically estimate a clip value (which is similar
#in this case, but need not be in general).

maghist(rnorm(1e4), xlim='auto', verbose = FALSE)``````

``````
#Test of log histograms:

testdata=10^(runif(1e3,0,4))
maghist(testdata)
#> [1] "Summary of used sample:"
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
#>    1.005   10.600   97.882  970.827  842.495 9893.141
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 1888.6374  141.8079  972.3256 3684.0541
#> [1] "Using 1000 out of 1000"``````

``````maghist(testdata,log='x')
#> [1] "Summary of used sample:"
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
#> 0.001991 1.025305 1.990641 1.978397 2.925567 3.995334
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 1.126441 1.407976 1.335352 1.878412
#> [1] "Using 1000 out of 1000"``````

``````maghist(testdata,log='y')
#> [1] "Summary of used sample:"
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
#>    1.005   10.600   97.882  970.827  842.495 9893.141
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 1888.6374  141.8079  972.3256 3684.0541
#> [1] "Using 1000 out of 1000"``````

``````maghist(testdata,log='xy')
#> [1] "Summary of used sample:"
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
#> 0.001991 1.025305 1.990641 1.978397 2.925567 3.995334
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 1.126441 1.407976 1.335352 1.878412
#> [1] "Using 1000 out of 1000"``````

``````
maghist(testdata,freq=FALSE)
#> [1] "Summary of used sample:"
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
#>    1.005   10.600   97.882  970.827  842.495 9893.141
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 1888.6374  141.8079  972.3256 3684.0541
#> [1] "Using 1000 out of 1000"``````

``````maghist(testdata,freq=FALSE,log='x')
#> [1] "Summary of used sample:"
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
#> 0.001991 1.025305 1.990641 1.978397 2.925567 3.995334
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 1.126441 1.407976 1.335352 1.878412
#> [1] "Using 1000 out of 1000"``````

``````maghist(testdata,freq=FALSE,log='y')
#> [1] "Summary of used sample:"
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
#>    1.005   10.600   97.882  970.827  842.495 9893.141
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 1888.6374  141.8079  972.3256 3684.0541
#> [1] "Using 1000 out of 1000"``````

``````maghist(testdata,freq=FALSE,log='xy')
#> [1] "Summary of used sample:"
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
#> 0.001991 1.025305 1.990641 1.978397 2.925567 3.995334
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 1.126441 1.407976 1.335352 1.878412
#> [1] "Using 1000 out of 1000"``````

``````
#Test of plotting histogram objects:

testhist=maghist(testdata,log='xy')
#> [1] "Summary of used sample:"
#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
#> 0.001991 1.025305 1.990641 1.978397 2.925567 3.995334
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 1.126441 1.407976 1.335352 1.878412
#> [1] "Using 1000 out of 1000"``````

``maghist(testhist)``

``maghist(testhist,log='x')``

``magplot(testhist,log='y')``

``magplot(testhist,log='xy')``

``````
#Nice to see a grid with large ranges:

maghist(rnorm(1e6), grid=TRUE)
#> [1] "Summary of used sample:"
#>      Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
#> -4.798760 -0.672827  0.000144  0.000752  0.675977  4.768394
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 1.0001413 0.9998855 0.9993710 2.0024501
#> [1] "Using 1000000 out of 1000000"``````

``````maghist(rnorm(1e6), log='y', grid=TRUE)
#> [1] "Summary of used sample:"
#>      Min.   1st Qu.    Median      Mean   3rd Qu.      Max.
#> -4.568203 -0.672258  0.002986  0.001804  0.676802  5.046734
#> [1] "sd / MAD / 1-sig / 2-sig range:"
#> [1] 0.9998797 1.0001524 1.0000371 2.0002543
#> [1] "Using 1000000 out of 1000000"``````

An example of using magbin:

``````set.seed(666)
xydata = cbind(rnorm(1e4), rnorm(1e4))
magbin(xydata, shape='hexagon') #default``````

``magbin(xydata, shape='square')``

``magbin(xydata, shape='triangle')``

``magbin(xydata, shape='trihex')``

``magbin(xydata, shape='hexagon', direction='v')``

``magbin(xydata, shape='triangle', direction='v')``

``magbin(xydata, shape='trihex', direction='v')``

``magbin(xydata, shape='hexagon', step=c(0.2,0.4)) #different aspect ratio hexagons``

``````
magbin(xydata, z=xydata[,1]^2-xydata[,2]^2, colref='zstat', sizeref='count')``````

``````
magbin(xydata, z=xydata[,1]^2-xydata[,2]^2, colref='zstat', sizeref='count',
``````magbin(xydata, z=xydata[,1]^2-xydata[,2]^2, colref='zstat', sizeref='count',
``````
``````magbin(xydata, z=xydata[,1]*xydata[,2], colref='zstat', sizeref='count',
``magbin(xydata, log='xy', unlog='xy', xlim=3, ylim=3)``