generated from sipbs-compbiol/sipbs-compbiol-book-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webr-example.qmd
126 lines (89 loc) · 2.77 KB
/
webr-example.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
---
webr:
packages: ["ggplot2", "tidyverse", "palmerpenguins", "GGally"]
filters:
- webr
---
# `R` Playground
```{r}
#| context: setup
library(ggplot2)
library(palmerpenguins)
library(tidyverse)
```
## Introduction
This page provides a `WebR` cell for you to use as a playground to experiment with some example datasets. You can use this page to explore data management and visualisation in `R`.
## Playground
```{webr-r}
# Use this WebR cell to experiment with some practice biological datasets
```
## Things you can do
This `WebR` instance has three packages installed:
- `ggplot2`
- `tidyverse`
- `palmerpenguins`
Open the callout boxes below to see some examples you can try in the code cell above.
::: { .callout-tip collapse="true" }
## Investigate Palmer's Penguins
The `penguins` dataset contains data about three different species of penguins. Take a look at the format of the dataset:
```r
glimpse(penguins)
```
You'll see there are eight variables, including `species`, `weight`, `sex`, etc. - some of these variables are _categorical_ (i.e. a category, like `species`), and others are _continuous_ (i.e. numerical). You can see a visual overview of how the data is related using the `plot()` function:
```r
plot(penguins)
```
We can visualise the number of penguins of each species in a bar chart:
```r
fig <- ggplot(penguins, aes(species, fill=species)) +
geom_bar()
fig
```
And break this down in a facet plot, by sex:
```r
fig <- ggplot(penguins, aes(species, fill=species)) +
geom_bar() +
facet_wrap(~sex)
fig
```
We can make a box and whisker plot of penguin body mass by species:
```r
fig <- ggplot(penguins, aes(x=species, y=body_mass_g, fill=species)) +
geom_boxplot()
fig
```
And plot the body mass for each sex side-by-side
```r
fig <- ggplot(penguins, aes(x=species, y=body_mass_g, fill=sex)) +
geom_boxplot()
fig
```
We can investigate correlations, such as between body mass and flipper length:
```r
fig <- ggplot(penguins, aes(x=body_mass_g, y=flipper_length_mm)) +
geom_point()
fig
```
We can colour datapoints by species:
```r
fig <- ggplot(penguins, aes(x=body_mass_g, y=flipper_length_mm, colour=species)) +
geom_point()
fig
```
And fit a linear regression to each species separately:
```r
fig <- ggplot(penguins, aes(x=body_mass_g, y=flipper_length_mm, colour=species)) +
geom_point() +
geom_smooth(method="lm")
fig
```
:::
::: { .callout-note }
`R` comes with a number of example datasets you can practice with, including:
- `mtcars`: fuel consumption and other statistic for 32 automobiles
- `Titanic`: information on the fate of passengers on the fatal maiden voyage of the ocean liner _Titanic_
You can see a full list by running the command
```r
library(help = "datasets")
```
:::