lowndrul
lowndrul

Reputation: 3815

Plot margin of pdf plot device: y-axis label falling outside graphics window

I tried simply plotting some data in R with the y-axis label horizontal and left of the y-axis tick labels. I thought the code below would work:

set.seed(1)
n.obs       <- 390
vol.min     <- .20/sqrt(252 * 390)
eps         <- rnorm(n = n.obs, sd = vol.min)
mar.default <- c(5,4,4,2) + 0.1
par(mar = mar.default + c(0, 4, 0, 0))               # add space to LHS of plot
pdf("~/myplot.pdf", width=5.05, height=3.8)
plot(eps,  main  =  "Hello  World!", las=1, ylab="") # suppress the y-axis label
mtext(text="eps", side=2, line=4, las=1)             # add horiz y-axis label
                                                     # 4 lines into the margin

my image

Instead, as you may see, the y-axis label almost fell completely outside of the graphics window. This phenomenon still exists no matter how much I expand the LHS margin.

Q: What am I doing wrong? Is there something I need to do with the oma parameter? What do I need to do to plot things the way I'm intending? I'm a little overwhelmed by all of this!

Upvotes: 25

Views: 29543

Answers (1)

joran
joran

Reputation: 173737

This is a classic one, maybe should be a FAQ. You have to set the par settings after the call to pdf, which creates the plotting device. Otherwise you're modifying the settings on the default device:

set.seed(1)
n.obs       <- 390
vol.min     <- .20/sqrt(252 * 390)
eps         <- rnorm(n = n.obs, sd = vol.min)
              # add space to LHS of plot
pdf("~/myplot.pdf", width=5.05, height=3.8)
mar.default <- c(5,4,4,2) + 0.1
par(mar = mar.default + c(0, 4, 0, 0)) 
plot(eps,  main  =  "Hello  World!", las=1, ylab="") # suppress the y-axis label
mtext(text="eps", side=2, line=4, las=1)   
dev.off()

enter image description here

Upvotes: 38

Related Questions