2

I want to increase distance between y-axis label and axis numbers (in the example: more space between "Height" and "1.5"), and also have more space to the left of the axis label.

Changing mar and oma in par does not do anything. Some similar questions here and here, but because they had no reproducible example, it did not solve my problem. I am not using any package.

Here are my codes:

Height = c(3.1, 0.4, 0.9, 2.6, 1.4, 2.1)
Diameter = c(1.0, 0.4, 0.8, 1.1, 0.5, 0.4)
size.data = data.frame(Height, Diameter)

par(mfrow = c(1, 1), mar = c(4, 5, 6, 1), oma = c(0.5, 1, 1, 0.5), mgp = c(2.2, 0.7, 0))
png('Figure.1.png', width = 2800, height = 2400, res = 220)
plot(Height ~ Diameter, data = size.data, xaxs = 'i', yaxs = 'i',
ylim = c(0, 3), xlim = c(0, 1.5), bty = 'n', box = FALSE, 
xlab = 'Diameter', ylab = 'Height', cex = 1.3, cex.axis = 1.3, cex.lab = 1.3, cex.main = 1.3,
pch = 21, bg = 'white', las = 1)
#mtext('Height', side = 2, at = 101, line = 1.1, cex = 1.3)
box(bty = 'L')
dev.off()

par(mfrow = c(1, 1), mar = c(4, 10, 6, 1), oma = c(0.5, 4, 1, 0.5), mgp = c(2.2, 0.7, 0))
png('Figure.2.png', width = 2800, height = 2400, res = 220)
plot(Height ~ Diameter, data = size.data, xaxs = 'i', yaxs = 'i',
ylim = c(0, 3), xlim = c(0, 1.5), bty = 'n', box = FALSE, 
xlab = 'Diameter', ylab = 'Height', cex = 1.3, cex.axis = 1.3, cex.lab = 1.3, cex.main = 1.3,
pch = 21, bg = 'white', las = 1)
#mtext('Height', side = 2, at = 101, line = 1.1, cex = 1.3)
box(bty = 'L')
dev.off()

Changing mar and oma does not do anything and generates exactly the same plot!

Figure.1

Figure.1

Figure.2

Figure.2

I tried setting the y-axis label separately using mtext but because there is not enough margin, which I am trying to change, it did not display either. I am not sure what basic thing I am missing and I appreciate any help.

Henrik
  • 65,555
  • 14
  • 143
  • 159
owl
  • 1,841
  • 6
  • 20
  • 30
  • 1
    From `par`: "Each device [e.g. png] has its own set of graphical parameters.". Thus, `png` resets the `par`. – Henrik Jul 16 '20 at 01:20
  • That makes sense! Thanks for adding this. – owl Jul 16 '20 at 01:26
  • 1
    People have been bitten before: [Set plot margin in png plot device](https://stackoverflow.com/questions/14878217/set-plot-margin-in-png-plot-device) ;) – Henrik Jul 16 '20 at 01:27
  • Ah, I did not see that post as it did not occur to me that it was png() related. Hard to come up with good keywords to search R-related issues (and titles when I post questions). – owl Jul 16 '20 at 01:36

1 Answers1

3

You must make your calls to par after you call png.

png('Figure.1.png', width = 2800, height = 2400, res = 220)
par(mfrow = c(1, 1), mar = c(4, 5, 6, 1), oma = c(0.5, 1, 1, 0.5), mgp = c(2.2, 0.7, 0))

AND

png('Figure.2.png', width = 2800, height = 2400, res = 220)
par(mfrow = c(1, 1), mar = c(4, 10, 6, 1), oma = c(0.5, 4, 1, 0.5), mgp = c(2.2, 0.7, 0))

The plots then look like this:

Figure 1

Figure 2

G5W
  • 36,531
  • 10
  • 47
  • 80
  • Ha! I knew it was something really basic, if you can figure that out. Thank you, I would not have been able to figure this out on my own. – owl Jul 16 '20 at 01:15