Consider the following example:
dat <- matrix(rexp(240, rate=.1), ncol=6)
par(mfrow=c(3,2))
for (i in 1:6){
plot(dat[,i],xlab = "Day of year",col = "black",
ylab = expression(paste("Temperature ",degree,"C")))
}
This produces 6 subplots where the x and y label are clearly shown.
I would like to alter this so that the ylabel is only shown on the panels which are on the right (to avoid repetition). So, I write:
for (i in 1:6){
if (i %% 2 == 0){
plot(dat[,i],ylab = "",xlab = "Day of year",col = "black")
}
else {plot(dat[,i],ylab = expression(paste("Temperature ",degree,"C")),
xlab = "Day of year",col = "black")
}
}
Which produces:

Where we can see that only the even numbered panel now have the ylabel. I would also like to have the figure so that only panel 5 and 6 has the xlabel, which I could so by adding another if statement. However, it seems a lot of work to do something so simple. Can anyone suggest an alternative method. As I am a beginner with R I would prefer to use some of the R base functions first i.e. not lattice nor ggplot. However, if there isn’t a cleaner way of doing this with the base functions, other suggestions are welcomed.
ifelseis a helpful shortcut.Also, you can use
mtextwithouter=TRUEif all axes are the same. After you plot everything with no labels, try: