## Background

Here is an example of a document that produces a plot from some data that's stored separately.

The data in @fig-timeseries shows the daily interactions with the moodle page for my second year lab classes. Can you spot the two dominant patterns in the data?

## an actual computed figure

```{r}
#| warning: false
#| echo: false
#| label: fig-timeseries
#| fig-cap: "A line plot of some mystery data"

# r code
# schluppeck, 2022-12-12

library(tidyverse)

df <- read_csv("2017-mysteryTimeseries.csv")

df %>%
    ggplot(aes(x = theTime_day, y = n)) +
        geom_line(linewidth=1) +
        scale_x_date(
            limits = as.Date(
                c('2016-09-01', '2017-01-01'))) +
        labs(x = "Date", y = "# interactions") +
        theme_minimal()

```

## or a table

A badly formatted table... `rstats` with various packages handles tabular data much more nicely!

```{r}
head(df)
```

## or some further analysis

If you want to compute things for including **in your text**, so-called inline code, then you can make your code spit out markdown that's been patched up. 

E.g. figure out the total # of interactions:

```{r eval=FALSE}
#| code-fold: false
`r sum(df$n)`
```

Or in text: the total number of interactions were `r  sum(df$n)`, but mean and median were   `r  round(mean(df$n),3)`, and  `r  round(median(df$n),3)` respecively.

For code cells, if you turn `#| echo: true` to `false`, then the code is hidden!

We can also elaborate on previous plots, by adding additional analysis. @fig-hist shows the days, where `n>100`.

```{r}
#| echo: false
#| label: fig-hist
#| fig-cap: "Events with > 100 interactions labelled"

df %>%
    ggplot(aes(x = n)) +
        geom_histogram(bins = 30, fill=2) +
        scale_fill_brewer(type='qual',1 ) +
        geom_vline(xintercept = 100) +
        labs(x = "# interactions", y = "count") +
        theme_minimal()
```


## Notes 

- `dplyr`
- Check out how conveniently the output format can be swapped out with `quarto render 01-doc-with-julia.qmd --to pdf` etc