0

How can I add labels in a plot where I've used the function plot and points?

I have the following code

    x<-seq(0,1,.01)
    alpha=2
    beta=3
    y<-dbeta(x,alpha,beta)
    plot(x,y,ylim=c(0,5.9))
    n=10
    y1<-dbeta(x,alpha+x,beta+n-x)
    points(x,y1)

And the display is

enter image description here

I would like to indicate with labels this is prior and this is posterior .Also indicating which parameters are being used.

I know how to add labels when you only use plot(x,y,xlab="y axis")

but not when combined with points also this form of labeled would not be that clear in the plot.

The labels not in the usual form as is x and y labels in the plot, but indicating inside the plot, this is the prior and this the posterior.

Could you please help?

Thank you in advance.

user9802913
  • 245
  • 4
  • 20
  • you can use the `text()` function...something like `text(x = .2, y = 5, label = "this is posterior")`. You can also use `paste0()` or related function to incorporate the parameter values, i.e. something like `paste0("This is the posterior. Alpha = ", alpha, " Beta = ", beta)`. You could also pass in parameter values for x and y to automatically place them in reasonable places. – Chase Jan 23 '19 at 04:15
  • And check out [this post](https://trinkerrstuff.wordpress.com/2018/03/15/2246/) to incorporate Greek symbols... – Chase Jan 23 '19 at 04:16
  • @Chase `text()` works fine, thank you! .I think `paste0()` does nothing in the plot, I don't see it. – user9802913 Jan 23 '19 at 04:29
  • 1
    I think `paste0` needs to be used inside `text()`. Its just so you can have more automated labesl – morgan121 Jan 23 '19 at 04:39
  • yes @RAB - that's right...would need to be passed to the label parameter inside `text()` – Chase Jan 23 '19 at 04:42

1 Answers1

1

By the comment of @Chase,

Using the function text().

Example:

text(x = .2, y = 5, label = "Posterior distribution").

user9802913
  • 245
  • 4
  • 20