How to move y-axis labels away from R plot using lapply in R

Brennan

I have the following code (Thanks to an answer from @Rawr in this question):

labes1 <- c("P(LNG)","","Volume(LNG)","","P(oil)","","Can.GDP","","US GDP","")
titles <- c("Levels","","","","","Log Difference","","","","")

par(mfrow = c(5, 2), mar = c(0.3, 6, 0, 2), oma = c(5, 0, 3, 2))
lapply(1:10, function(ii) {
  x <- plotdata1[, ii, drop = FALSE]
  plot(x, xlab = "Quarter", ylab = labes1[ii], axes = FALSE)
  axis(2, las = 1)
  box()
  if (ii %in% 9:10) {
    axis(1)
    title(xlab = 'Quarter', xpd = NA)
  }
  if (ii %in% 1:2)
    title(main = c('Levels', 'Log Difference')[ii], xpd = NA, line = 1)
})

This produces the following plot:

enter image description here

The obvious issue is the overlaying of the y-axis labels with the y-axis values. I have tried playing around with the mar() and oma() but these just change the margins around, I was hoping this would move things out of the way. How can I move the y-axis labels as separate from the plot? I will also be moving the margins a bit so that the white space between the two columns of plots will be closer together.

RoB

You can define the ylab separately, like what you're doing for the xlab, and set the line parameter to define its distance from the plot (as stated in this post).

I got a running example from combining your code and @rawr's from your previous question.

set.seed(1)
z <- ts(matrix(rt(200 * 10, df = 3), 200, 10), start = c(1961, 1), frequency = 12) 
z <- z * 1e5 # to make "wide" y-axis labels

## vectors of x, y, and main labels
xl <- sprintf('x label %s', 1:10)
yl <- sprintf('y label %s', 1:10)
ml <- sprintf('main label %s', 1:10)


labes1 <- c("P(LNG)","","Volume(LNG)","","P(oil)","","Can.GDP","","US GDP","")
titles <- c("Levels","","","","","Log Difference","","","","")

par(mfrow = c(5, 2), mar = c(0.3, 6, 0, 2), oma = c(5, 0, 3, 2))
lapply(1:10, function(ii) {
  x <- z[, ii, drop = FALSE]
  plot(x, xlab = "Quarter", ylab = "", axes = FALSE) # set ylab to ""
  axis(2, las = 1)
  title(ylab = labes1[ii], line = 4) # set the line at an appropriate distance 
  box()
  if (ii %in% 9:10) {
    axis(1)
    title(xlab = 'Quarter', xpd = NA)
  }
  if (ii %in% 1:2)
    title(main = c('Levels', 'Log Difference')[ii], xpd = NA, line = 1)
})

The code above outputs the following graph for line = 4 :

enter image description here

and this plot for line = 3 :

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

R barplot: how to move x-axis' tick labels farther away from the axis

How to move tick mark labels away from axis using boxplot()

How to extract y axis labels from boxplot in R?

R: how to move axes labels closer to the plot

Plot with R - labels on x axis

How can I move x-axis labels away from the centre of a ggplot that uses coord_polar?

R plot. Celsius on y axis with exact Fahrenheit conversion of the y labels on second y axis

How to plot bar plot for factor variales using lapply in R?

R sjplot: How to angle the X axis labels and move them down?

How to plot on R multiple variables on the X axis, and their values on the Y axis?

How do you increase the space between the axis labels and axis titles in R using functions ggplot() and plot_ly?

How to plot standard deviation in a two way y axis plot in r?

How to shift bars from y-axis using barplot() R

Seasonal Plot change of labels on the x axis in R

Missing x axis labels in R plot

How to remove the Y-axis labels, ticks and axis label in a plot created using librosa.display.specshow

R how to increase spacing between X and Y axis value labels

plot 2 variables on Y axis, using ggvis in R

Scaling dual y axis plot in R using ggplot

Plot multiple variable on y axis in R using ggplot

plot y-axis with the actual value from the hierarchical clustering in R

Remove axis labels from a plot in R after the plot has already been created

R how to automatically adjust y axis when using basic plot with xlim

How to create a common title in X and Y axis in an arrange of plots using `ggdraw` and `plot_grid()` in R?

How to build correlation matrix plot using specified multiple variables on x and y axis in r

How can I keep grid.arrange from cutting off x-axis labels on 4 panel plot in R?

How to plot (almost) the same function at both sides of the "y" axis in R?

R: How can I set the y axis in radians in a plot?

How to extract y-axis value on R plot?