---
title: "Graphs example code"
author: "Statistical Consulting Centre"
output: 
  html_document:
    toc: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r, message=FALSE}
library(tidyverse)
library(janitor)
library(readxl)
```

### Reading in the data
 
The Excel file read in this example is analytic_data.xlxs.  Replace this with your Excel file.

In this R Markdown file, the data frame is called EXAMPLE_DATA.  Replace this with the name of the file you wish to use.

```{r, message=FALSE}
EXAMPLE_DATA <- read_excel("analytic_data.xlsx")
EXAMPLE_DATA <- EXAMPLE_DATA %>% 
  mutate_if(is.character,as.factor)
```

In all of the code below, you will need to replace EXAMPLE_DATA with the name of your data frame.
You will need to use the appropriate variable names.

### Bar chart

```{r}
ggplot(EXAMPLE_DATA, aes(y=CATEGORICAL_VARIABLE2)) + 
  geom_bar(width = 0.5) +
  labs(y="NICE AXIS LABEL", x = "Frequency")
```


### Scatterplot

```{r}
ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1, y = NUMERICAL_VARIABLE2)) +
  geom_point() +
  labs(x="NICE X-AXIS LABEL", y = "NICE Y-AXIS LABEL") 
```

Add a smoother.

```{r}
ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1, y = NUMERICAL_VARIABLE2)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  labs(x="NICE X-AXIS LABEL", y = "NICE Y-AXIS LABEL") 
```

Add a linear regression line.

```{r}
ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1, y = NUMERICAL_VARIABLE2)) +
  geom_point() +
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE) +
  labs(x="NICE X-AXIS LABEL", y = "NICE Y-AXIS LABEL") 
```

Add groups

```{r}
ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1, y = NUMERICAL_VARIABLE2, colour=CATEGORICAL_VARIABLE1)) +
  geom_point() +
  labs(x="NICE X-AXIS LABEL", y = "NICE Y-AXIS LABEL") 
```


### Histogram

You can edit the number of bins, using 'bins ="; between 5 and 15 can be useful.

```{r}

ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1)) +
      geom_histogram(bins = 15) +
      labs(x="NICE AXIS LABEL") 

```

### Boxplot


```{r}
ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1)) + 
      geom_boxplot() + 
      labs(x="NICE AXIS LABEL") +
      scale_y_continuous(breaks=NULL) 

```

### Time series plot


```{r}
ggplot(EXAMPLE_DATA, aes(x=TIME_VARIABLE, y = NUMERICAL_VARIABLE1)) +
  geom_point() +
  geom_line() +
  labs(x="Time", y = "NICE Y-AXIS LABEL")
```

### Dot chart / lollypop

```{r}
freqTable <- EXAMPLE_DATA %>%
  tabyl(CATEGORICAL_VARIABLE2)

ggplot(freqTable, aes(x = n, y = CATEGORICAL_VARIABLE2)) + 
  geom_point(size=3) +
  geom_segment(aes(x=0, 
                   xend=n, 
                   y=CATEGORICAL_VARIABLE2, 
                   yend=CATEGORICAL_VARIABLE2)) +
  labs(x = "Frequency", y = "NICE AXIS LABEL")
```


### Adding a categorical grouping to a plot of a numerical variable 
```{r}
ggplot(EXAMPLE_DATA, aes(y = CATEGORICAL_VARIABLE1, x = NUMERICAL_VARIABLE1)) + 
        geom_boxplot() +
        labs(y ="NICE Y-AXIS LABEL", x = "NICE X-AXIS LABEL")
```

### Using facet_wrap for additional categorical grouping

```{r}
ggplot(EXAMPLE_DATA, aes(y = CATEGORICAL_VARIABLE1, x = NUMERICAL_VARIABLE1)) + 
        geom_boxplot() +
        labs(y ="NICE Y-AXIS LABEL", x = "NICE X-AXIS LABEL") +
  facet_wrap(vars(CATEGORICAL_VARIABLE2), nrow=2)
```


© Statistical Consulting Centre, University of Melbourne, 2023