Some notes on adjusting margins and using the mtext() function to place axis labels closer the axes.

Standard plot 1 panel plot

Issues

  • Large margin above the plot
  • Large distance between labels on the tick marks and the axis label
data(iris)
with(iris, plot(Sepal.Length ~ Petal.Length))




Change margins around plot

  • use “par(mar = c(lower, left,top,right))”
  • defult is mar = c(5, 4, 4, 2) + 0.1.

Here, I decrease the top and left margins to 1.

par(mar = c(5,4,1,1))
with(iris, plot(Sepal.Length ~ Petal.Length))




Move axis labels closer to axis

  • set axis labels to xlab = “” and ylab = “”
  • Add new labels using mtext()
  • mtext stands for “margin text”
  • mtext takes several arguements:

Here, I decrease the top and left margins to 1.

Change axis lables

with(iris, plot(Sepal.Length ~ Petal.Length,
      xlab = "Petal Length",
      ylab = "Sepal length"))

Plot with no axis labels

  • ylab = “” sets it to print nothing
with(iris, plot(Sepal.Length ~ Petal.Length,
      xlab = "",
      ylab = ""))

Plot with x axis added with mtext()

  • side = 1 sets it to the lower maring
  • side = 2 would do the left
  • side= 3 top, side = 4 right
  • line = 2 sets it 2 lines below the axis
  • it usually takes some trial and error to get it where you want it
with(iris, plot(Sepal.Length ~ Petal.Length,
      xlab = "",
      ylab = ""))

#add text 
mtext(text = "Petal Length",
      side = 1,#side 1 = bottom
      line = 2)

Plot with y axis added with mtext()

  • side = 2 would do the left
with(iris, plot(Sepal.Length ~ Petal.Length,
      xlab = "",
      ylab = ""))

# x axis
mtext(text = "Petal Length",
      side = 1,line = 2)

# y axis

mtext(text = "Sepal Length",
      side = 2, #side 2 = left
      line = 2)

Reset margins

  • Get rid of blank space by changing margins
  • set mar = c(2,2,1,1)
par(mar = c(3,3,1,1))

with(iris, plot(Sepal.Length ~ Petal.Length,
      xlab = "",
      ylab = ""))

# x axis
mtext(text = "Petal Length",
      side = 1,line = 2)

# y axis

mtext(text = "Sepal Length",
      side = 2, #side 2 = left
      line = 2)

No tick marks & tick labels

If for some reason you want no tick marks and no numbers along the axies, you can set par(yaxt = “m”)

#change par
par(yaxt="n",#remove from y
    xaxt='n')#remove from x


with(iris, plot(Sepal.Length ~ Petal.Length))

Set it back tiwht yaxt = “t”

#change par
par(yaxt="t",#remove from y
    xaxt='t')#remove from x


with(iris, plot(Sepal.Length ~ Petal.Length))