-
Notifications
You must be signed in to change notification settings - Fork 0
/
2 - MWE.Rmd
71 lines (50 loc) · 1.82 KB
/
2 - MWE.Rmd
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
---
title: "2 - MWE"
output: html_document
---
```{r setup, include=FALSE}
# I like to put all of my attached libraries up here
library(tidyverse)
# Make a color palette
colorRamp <- viridisLite::viridis(n=2)
knitr::opts_chunk$set(echo = TRUE)
options(stringsAsFactors=FALSE)
```
# Minimum Working Examples - MWE
In *1-Basic.Rmd* we used a default dataset (cars) included in R to test drive Rmarkdown. But let's try creating our own test data. Much of this material is taken from Jared Knowles' blog: <https://www.jaredknowles.com/journal/2013/5/27/writing-a-minimal-working-example-mwe-in-r>.
Following, we will make a basic data frame of simulated pollock lengths.
```{r data}
dataRows <- 250
lengthData <- data.frame(id = seq(1, dataRows),
sex = sample(c("male", "female"), size = dataRows, replace = TRUE),
length = abs(rnorm(dataRows, mean = 357, sd = 169))
)
head(lengthData)
```
# Make a table
There are several packages that help you format tables (<https://rmarkdown.rstudio.com/lesson-7.html>). The function *kable* is included in the package **knitr** which is already an Rmarkdown dependency.
```{r table1}
summary <- lengthData %>%
group_by(sex) %>%
summarize(Mean = mean(length),
SD = sd(length),
Count = n()
)
knitr::kable(summary)
```
<br>
Here is an example of in-line R code: there are `r dataRows` rows of data in this table.
# GGplot
Here are a couple of ggplot outputs. Note that we use the color palette created above.
```{r boxplot}
ggplot(lengthData, aes(x=sex,y=length, fill=sex)) +
geom_boxplot() +
scale_fill_manual(values=colorRamp) +
theme_bw()
```
```{r histogram}
ggplot(lengthData, aes(x=length, fill=sex)) +
geom_histogram(bins=40, position="dodge") +
scale_fill_manual(values=colorRamp) +
theme_bw()
```