Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ venv
.DS_Store
.Rhistory
.Rdata
*.html
13 changes: 4 additions & 9 deletions Class_10/Class_10.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
title: "Class 10"
author: "Szymon Drobniak"
date: "`r Sys.Date()`"
output:
md_document:
toc: true
variant: markdown_github
output: rmdformats::robobook
---

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

# Class 10

**In the below instructions...**

**EXERCISE 1:** are bits of code to execute/practice pieces to do, often with only hints on how to perform them.
Expand Down Expand Up @@ -329,7 +324,7 @@ Control over the graphical parameters in the `plot()` function is rudimentary. T
A simple *ggplot2* graph may be structured as follows:

```{r eval = FALSE, include = T, echo = T}
mygraph <- ggplot2(data = MYDATA,
mygraph <- ggplot(data = MYDATA,
mapping = aes(x = VAR1, y = VAR2, ...)) +
geom_1(OPTIONS) +
geom_2(OPTIONS)
Expand All @@ -342,7 +337,7 @@ plot(graph2)

Calling the `ggplot()` function may be used only to create an object of class `ggplot`, without displaying the actual graph. Such object will contain the data and its mappings to specific elements of the final plot. To display it, we need additional function from the `geom_...` family, which add specific visual elements to the defined mappings (e.g. `geom_point` adds scatterpoints, `geom_hist` forms a histogram). Subsequent elements can be concatenated using the `+` operator. other elements that can be added to the plot using `+` are display and aesthetic rules, e.g. `theme()`, which describe the appearance of non-data elements of a plot.

Load the `ggplot2` - if you don;t have it use `install.packages()` to install it.
Load the `ggplot2` - if you don't have it use `install.packages()` to install it.

```{r echo = T}
# install.packages('ggplot2')
Expand Down
86 changes: 79 additions & 7 deletions Class_12/Class_12.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
title: "Class 12"
author: "Szymon Drobniak"
date: "`r Sys.Date()`"
output:
md_document:
toc: true
variant: markdown_github
output: rmdformats::robobook
---

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

# Class 12

**In the below instructions...**

**EXERCISE 1:** are bits of code to execute/practice pieces to do, often with only hints on how to perform them.
Expand Down Expand Up @@ -393,6 +388,83 @@ myplot <- ggplot(data = mydata, mapping = aes(x = Height, y = weight6weeks, colo
myplot
```

## Other geometries

### Barplot

```{r echo = T}
myplot <- ggplot(data = mydata, mapping = aes(x = as.factor(Diet),
fill = as.factor(gender),
y = ..count..)) +
geom_bar() +
labs(x = "Diet type", y = "Count", fill = "Diet") +
scale_x_discrete(labels = c('vegan','lacto-ovo', 'vegeterian')) +
scale_fill_discrete(labels = c('f', 'm')) +
theme_classic() +
theme(text = element_text(size = 20))

# ggplot2 automatically counts things (..count.. is optional)

myplot

myplot <- ggplot(data = mydata, mapping = aes(x = as.factor(Diet),
fill = as.factor(gender),
y = ..count..)) +
geom_bar(position = "dodge") +
labs(x = "Diet type", y = "Count", fill = "Diet") +
scale_x_discrete(labels = c('vegan','lacto-ovo', 'vegeterian')) +
scale_fill_discrete(labels = c('f', 'm')) +
theme_classic() +
theme(text = element_text(size = 20))

# ggplot2 automatically counts things (..count.. is optional)

myplot
```


### Error bars

```{r echo =T}
library(tidyverse)

summ <- tibble(Age = levels(data_chol$AgeGroup),
mean = by(data_chol$After8weeks, data_chol$AgeGroup, mean, na.rm = T),
se = by(data_chol$After8weeks, data_chol$AgeGroup,
function(x) sd(x, na.rm = T)/sqrt(length(x))))

myplot <- ggplot(data = summ, mapping = aes(x = Age, y = mean,
ymin = mean-se, ymax = mean+se)) +
geom_bar(stat = 'identity', fill = 'white', colour = 'black') +
geom_errorbar(width = 0.5) +
theme_classic() +
theme(text = element_text(size = 20)) +
labs(y = "Cholesterol conc. after 8 weeks") +
scale_x_discrete(limits = c('Young', 'Middle', 'Old'))
myplot

myplot <- ggplot(data = summ, mapping = aes(x = Age, y = mean,
ymin = mean-se, ymax = mean+se)) +
geom_point(colour = 'black', cex = 5) +
geom_errorbar(width = 0.5) +
theme_classic() +
theme(text = element_text(size = 20)) +
labs(y = "Cholesterol conc. after 8 weeks") +
scale_x_discrete(limits = c('Young', 'Middle', 'Old'))
myplot

myplot <- ggplot(data = summ, mapping = aes(x = Age, y = mean,
ymin = mean-se, ymax = mean+se)) +
geom_point(colour = 'black', cex = 5) +
geom_errorbar(width = 0.5) +
theme_classic() +
theme(text = element_text(size = 20)) +
labs(y = "Cholesterol conc. after 8 weeks") +
scale_x_discrete(limits = c('Young', 'Middle', 'Old')) +
ylim(c(0, 6.5))
myplot
```


## Saving the plots

Expand Down
Loading