-1

I'm working through a code and cannot figure out how to influence this spacing..I've tried oma, mai etc. but cannot get this space smaller (the space between the plot and the axis description "Jan 2000" etc.). Which command do I need?

enter image description here

MichiZH
  • 5,587
  • 12
  • 41
  • 81
  • Very hard to help you. Can you please add a reproducible example? – agstudy Jul 22 '14 at 13:16
  • base plot. I cannot really put an example in here since I use a custom and big XTS file for these plots. – MichiZH Jul 22 '14 at 13:21
  • Please read about how to create a [**minimal reproducible example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – Henrik Jul 22 '14 at 13:23

2 Answers2

2

I see you mention that you have tried oma and mai, but have you tried mgp? You can vary the space between an individual axis and its tick labels using mgp.axis.labels, which requires the Hmsic package

Let's set up an example dataframe:

require(Hmisc)
A <- rnorm(3,1,100)
B <- c("january","february","march")
dat <- data.frame(B,A)

options('mpg.axis.labels') gives you the default distance of .7 for each axis

!> options('mgp.axis.labels')
 $mgp.axis.labels
 [1] 0.7 0.7

This gives you the 3 values for each axis, in this case we print the x axis default values:

mgp.axis.labels(type='x')
 [1] 3.0 0.7 0.0

Let's change the distance between tick labels and plot to approximately 1/3rd of what it was:

mgp.axis.labels(c(3.0,0.2,0.0), type='x')

plot(dat,axes=FALSE)
mgp.axis(1,at=1:3,labels=dat$B)

Done! There is now less distance between the X axis and the tick labels.

bjoseph
  • 2,116
  • 17
  • 24
0

So if you have an xts object, then you are using plot.xts(...). I think the default axis margins are set differently in plot.xts(...). Try playing with the mgp=... argument (see ?par for details).

library(xts)
data(sample_matrix)     # sample dataset in xts package
ts <- as.xts(sample_matrix, descr='my new xts object')
par(mfrow=c(1,2))
plot(ts$Open,auto.grid=F)
plot(ts$Open,auto.grid=F,mgp=c(3,1,0))

The plot on the left uses the defaults for plot.xts(...), the plot on the right sets mgp to the default in base R.

jlhoward
  • 58,004
  • 7
  • 97
  • 140