-3

I need to plot some numeric vectors in R but I would like to see the numbers on the graph. For example, let's suppose to plot a function with a line chart. I would like to visualize the number corresponding to max/min values of the function directly on the graph. Is it doable and how? Thanks.

opt
  • 477
  • 1
  • 10
  • 25
  • In general, yes you can to this, but you need to give a more concrete example of what your inputs are and what the finished graph should look like. – John Paul Oct 11 '14 at 17:34
  • Generally, the expectation on SO is that you show us what you've tried. See: [how-to-make-a-great-r-reproducible-example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) for a description of a well-posed question. – jlhoward Oct 11 '14 at 17:41
  • It's also an expectation tha tthe question will have made some sort of effort at searching both the Interweb and SO. – IRTFM Oct 11 '14 at 17:49

1 Answers1

4

Here an example using R base plots:

enter image description here

reproducible example

## you should set the seed with random data
set.seed(1)
x <- rnorm(100)

plot your data

## note the use of extrandrange to be sure that we have enough spaces 
## to plot and show your extrema
plot(x,type='l',ylim=extendrange(x))

extract extrema

## wich.max extreact the index of the extrema 
xm = c(which.max(x),which.min(x))
ym <- c(max(x),min(x))

plot extrema

## you should round the data for pretty formatting
text(xm,ym,label=round(ym,2),col='red',adj=c(-0.5))
points(xm,ym,col='green',pch=20,cex=2)
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • 1
    Upvoting you answer as good example of well-contructed answer, but voting to close as too broad because of the profusion of tags. There are already many R graphics tutorials if the questioner would only do a bi of searching. – IRTFM Oct 11 '14 at 17:48
  • @BondedDust I completely agree with you. I am waiting for The OP to improve his question otherwise I will also vote to close it. – agstudy Oct 11 '14 at 17:52