R: Add text to areas in the lower corner of the front area

I am drawing some graphs in baseR and I am trying to plot text at the bottom corner of my plot. I tried to use mtext()

but it doesn't give me the desired result. How would you do it? The idea, after all, is to generate something like the graph below. How can i do this?

enter image description here

Here is my code that I am using to create graphs.

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


source to share


2 answers


plot(1)
title(sub="hallo", adj=1, line=3, font=2)

      



0


source


With, mtext()

you can place your text in the graph field. In your case, you can try playing with the parameters line

and at

. Cm.help(mtext)

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

      



enter image description here

+5


source







All Articles