17

I am plotting multiple graphs in baseR and I am trying to plot a text in the lower rightern corner of my plots. I tried using mtext() but this doesn't give me the desired result. How would you do this? The idea in the end is to generate something like the graphic below. How could I do this?

enter image description here

Here is my code I use to generate the plots.

xy <- data.frame(NAME=c("NAME1", "NAME1","NAME1","NAME1","NAME2","NAME2","NAME2"),ID=c(47,47,47,47,259,259,259),YEAR=c(1932,1942,1965,1989,2007,2008,2014),VALUE=c(0,NA,-6,-16,0,-9,-28), test=c("text1","text1","text1","text1","text2","text2","text2"))

# split data by index 
ind <- split(x = xy,f = xy[,'ID'])

plot1 <- function(x) {
  fname <- paste0(x[1, 'ID'], '.png')
  png(fname, width=1679, height=1165, res=150)
  par(mar=c(6,8,6,5))
  plot(x = c(1946, 2014),
       y = range(x$VALUE, na.rm=TRUE),
       type='n',
       main=x[1, 'NAME'],
       xlab="Time [Years]",
       ylab="Value [m]")
  axis(2, at = seq(-100000, 100000, 100), cex.axis=1, labels=FALSE, tcl=-0.3)
  points(x[,c('YEAR','VALUE')], type="l", lwd=2)
  points(x[,c('YEAR','VALUE')], type="p", lwd=1, cex=0.5, pch=21, bg='white')
  abline(h=0)
  mtext(x$test, side=1, )

  dev.off()
}

plot2 <- function(x) {
  fname <- paste0(x[1, 'ID'], '.png')
  png(fname, width=1679, height=1165, res=150)    
  par(mar=c(6,8,6,5))
  plot(x[,c('YEAR','VALUE')],
       type='n',
       main=x[1, 'NAME'],
       xlab="Time [Years]",
       ylab="value [m]")
  axis(2, at = seq(-100000, 100000, 100), cex.axis=1, labels=FALSE, tcl=-0.3)
  points(x[,c('YEAR','VALUE')], type="l", lwd=2)
  points(x[,c('YEAR','VALUE')], type="p", lwd=1, cex=0.5, pch=21, bg='white')
  abline(h=0)
  mtext(x$test, side=1)
  dev.off() 
}

lapply(ind, function(x) ifelse(any(x$YEAR < 1946 & x$YEAR < 2014), plot2(x), plot1(x)))

3 Answers 3

23

With mtext() you can put your text at plot margin. In your case, you can try playing with parameters line and at. See help(mtext)

plot(1:10,10:1)
mtext('text is here', side=1, line=3.5, at=9)

enter image description here

4
  • thank you, I already tried this but the problem is that when I plot (multiple plots, not a single one!) in scenario 2 (in my sample code above), I have a x-axis that varies, so a fixed position with at won't really help....
    – kurdtc
    Commented Sep 18, 2014 at 8:22
  • The value to the argument at of mtext() does not necessarily have to be a fixed value. You can assign a variable to it according to your x-axis.
    – HongboZhu
    Commented Sep 18, 2014 at 8:49
  • so if I do something like this mtext(x$test, side=1, line=3.5, at=x$YEAR) how could I specify at=x$YEAR that it always places it at the last year?
    – kurdtc
    Commented Sep 18, 2014 at 8:53
  • just found out, that I can do it like this: mtext(x$test, side=1, line=3.5, at=last(x$YEAR))
    – kurdtc
    Commented Sep 18, 2014 at 9:02
18
plot(1:10,10:1)
text(c(0,6,9), -0.6, paste('hello world', c(1:3)), xpd=NA)

With the text() function you can take reference for positioning to the coordinates of your plot and you can plot several text elements at ones.

The xpd parameter allows you to choose between three possibilities where you want to plot your element (also available for other elements like points and lines):

  1. FALSE : only inside the plot
  2. TRUE : in the outer plotting area
  3. NA : everywhere on your plotting device
5
plot(1)
title(sub="hallo", adj=1, line=3, font=2)
2
  • 1
    this is a great solution, however is there a way I could make it look (font, font size etc) than the default xlab and ylab? E.g. make the title look plain?
    – kurdtc
    Commented Sep 18, 2014 at 8:49
  • It is possible to add cex.sub = 0.75 option to control font size
    – Colibri
    Commented Aug 25, 2020 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.