# Basic plotting In bayesplay: The Bayes Factor Playground

```knitr::opts_chunk\$set(
collapse = TRUE,
comment = "#>",
eval = TRUE
)
```
```library(bayesplay)
```

The `bayesplay` package includes functionality for basic plotting of prior and likelihood objects. These plots are all generated with the generic `plot()` function. The `plot()` function returns a `ggplot2` object. These plots are styled with sensible defaults; however, they can be further styled using standard `ggplot2` syntax.

## Examples

### Plotting likelihoods

To plot a likelihood, you first need to define a likelihood using the standard syntax (see basic usage).

In the example below, we define normal likelihood with a mean of 5.5 and a standard error of 32.35.

```norm_mod <- likelihood(family = "normal", mean = 5.5, sd = 32.35)
norm_mod
```

To plot this object we just use it as the input to the `plot()` function.

```plot(norm_mod)
```

As shown above, default axis labels are applied to the x and y axis. The x-axis limits are also automatically set to so that the full shape of the likelihood is visible.

An identical approach can also be used for plotting t likelihoods. First we define the likelihood.

```t_mod <- likelihood(family = "student_t", mean = 10, sd = 5, df = 15)
t_mod
```

We can then plot the likelihood using the `plot()` function.

```plot(t_mod)
```

### Plotting priors

The process for plotting priors is identical to plotting likelihoods. First we define the prior, and then we plot it. Sensible axis labels are applied to plots of priors.

For example, we can plot a point prior, typically used for a null model.

```point_prior <- prior(family = "point", point = 0)
plot(point_prior)
```

We can also plot a uniform prior.

```uniform_prior <- prior(family = "uniform", min = 10, max = 20)
plot(uniform_prior)
```

Or a normal prior.

```normal_prior <- prior(family = "normal", mean = 10, sd = 10)
plot(normal_prior)
```

The `plot()` function also behaves sensibly with, for example, half-normal priors. The height of the plot is scaled appropriately, and the plot is only show over the appropriate parameter range.

```half_normal_prior <- prior(family = "normal", mean = 10, sd = 10, range = c(10, Inf))
plot(half_normal_prior)
```

### Styling plots

The plots produced by the `plot()` function are standard `ggplot2` objects and, therefore, they can be styled using the standard `ggplot2` syntax.

First, we load the `ggplot2` package to gain access to it's styling functions.

```library(ggplot2)
```

The functions than then we used to modify the output for the `plot()` function. For example, axis labels can be changed with `ggplot2::labs()`

```plot(t_mod) + labs(x = "data", y = "likelihood", title = "t likelihood")
```

or axis limits can be changed using, for example, `ggplot2::xlim()`. Note that because scales have already been set by the `plot()` function, a warning message will be produced. This however, this message can be suppressed using standard `R` methods.

```plot(uniform_prior) + xlim(-100,100)
```

Or the plot can be themed using `ggplot2` themes such as `ggplot2::theme_linedraw()`

```plot(norm_mod) +
labs(title  = "normal likelihood") +
theme_linedraw()
```

For the advanced plot types like the prior and posterior plots and the visual comparison (see Advanced usage) it is also possible to change the colours and the linetypes.

For example, the default colours for these plots are blue and red solid lines.

```data_model <- likelihood("noncentral_d", d = 0, n = 20)

d_model1 <- extract_predictions(data_model * prior("cauchy", 0, .707))
d_model2 <- extract_predictions(data_model * prior("point", 0))
visual_compare(d_model1, d_model2)
```
```plot(extract_posterior(data_model * prior("cauchy", 0, .707)), add_prior = TRUE)
```

However, it is possible to change this with `ggplot2::scale_colour_manual()`

```visual_compare(d_model1, d_model2) +
scale_colour_manual(values = c("green", "blue"),
labels = c("d_model1", "d_model2"), name = "Model")
```
```plot(extract_posterior(data_model * prior("cauchy", 0, .707)), add_prior = TRUE) +
scale_colour_manual(values = c("green", "blue"),
labels = c("posterior", "prior"), name = NULL)
```

It is also possible to change the linetype with `ggplo2::scale_linetype_manual()`,

```visual_compare(d_model1, d_model2) +
scale_linetype_manual(values = c(1, 2),
labels = c("d_model1", "d_model2"), name = "Model")
```
```plot(extract_posterior(data_model * prior("cauchy", 0, .707)), add_prior = TRUE) +
scale_linetype_manual(values = c(1, 2),
labels = c("posterior", "prior"), name = NULL)
```

or to change both the colour and the linetype.

```visual_compare(d_model1, d_model2) +
scale_linetype_manual(values = c(1, 2),
labels = c("d_model1", "d_model2"), name = NULL) +
scale_colour_manual(values = c("grey", "black"),
labels = c("d_model1", "d_model2"), name = NULL)
```
```plot(extract_posterior(data_model * prior("cauchy", 0, .707)), add_prior = TRUE) +
scale_linetype_manual(values = c(1, 2),
labels = c("posterior", "prior"), name = NULL) +
scale_colour_manual(values = c("black", "black"),
labels = c("posterior", "prior"), name = NULL)
```

## Try the bayesplay package in your browser

Any scripts or data that you put into this service are public.

bayesplay documentation built on Sept. 9, 2021, 9:09 a.m.