-
Notifications
You must be signed in to change notification settings - Fork 2
/
rmarkdown2.Rmd
206 lines (148 loc) · 7.67 KB
/
rmarkdown2.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
---
title: "Baby Steps in Coding R"
description: |
This lesson follows on from ‘My First R Lesson using R Markdown’ where students were introduced to the layout of an R Markdown document. In this lesson we start to look at the R code itself and to identify the commands needed to draw a bar chart.
# author: []
date: 2022-02-26
output:
distill::distill_article:
self_contained: false
---
<!-- <div style="text-align: center"> -->
<div class="contributor_org">
`r xfun::embed_file(here::here("rmarkdown2", "markdown2.Rmd"), text = "Download .Rmd here")`
`r xfun::embed_file(here::here("rmarkdown2", "markdown2.docx"), text = "Download word document here")`
`r xfun::embed_file(here::here("rmarkdown2", "markdown2.pdf"), text = "Download pdf here")`
</div>
<!-- </div> -->
<div style="text-align: center">
<img src="rmarkdown2/feedback_second_markdown2.jpg" width="600px" min-width="300px" max-width="300px" />
</div>
```{r setup, include=FALSE}
knitr::opts_chunk$set
library(tidyverse)
```
Before we start the lesson, type your name where it says "write your name here" on line 3. Make sure you type between the speech marks.
Knit the file. Can you see your name at the top of the output file? Close the output file by clicking on the X
## Lesson Objectives
To become familiar with the layout of an R Markdown file
To practice editing the R code
To identify R commands needed to plot a bar chart
To produce an output file of my work using the 'knit' command
## Packages used in this lesson:
- `tidyverse`
## Success Criteria:
I can identify free text in the Markdown file
I can identify where to type the R code in the Markdown file
I can use some R commands to plot a bar chart
I can produce an output file of my work
## Keywords:
* R code chunk
* free text
* knit
* variable
## Practising editing the code in R Markdown
We are going to draw a bar chart showing the percentage of different blood groups in the UK population.
Then we are going to practice writing the title and the labels to explain the bar chart.
Finally, we are also going to practice changing the colour of the bars.
After making each change, click on the 'knit' command at the top of the pane. After a short wait you will see the output file of your work. Check that the changes you made to the code have come out on the output file as you wanted them. eg You may have added a title for your bar chart in the code chunk in the R Markdown file, but perhaps the title has not been printed in the output file. Go back to the R Markdown file to check you put speech marks around it.
## Our data
This is the data we are going to use for the bar chart
+--------------------------+----------+----------+----------+----------+
| Blood Group | O | A | B | AB |
+==========================+:========:+:========:+:========:+:========:+
| Percentage of population | 44 | 42 | 10 | 4 |
+--------------------------+----------+----------+----------+----------+
: Table 1: Percentage of population with each blood group
Now we will find out how to enter the data from the above table into R.
Copy the following two lines of R code into chunk1 below. Make sure you paste the text between the top and bottom 3 back ticks.
blood_group <- c('O','A','B','AB')
pop_percentage <- c(44,42,10,4)
The first line tells R to store these letters in a variable called blood_group
The second line tells R to store the percentages in a variable called pop_percentage.
```{r chunk1}
```
To check what R has stored in the variable blood_group, type blood_group inside code chunk2 and select the arrow on the far right of the chunk. R will tell you what it has stored. It should be the four blood groups
```{r chunk2}
```
Now check what R has stored in the variable pop_percentage by typing pop_percentage in code chunk3. Click on the arrow on the far right of the chunk. R will tell you what it has stored. It should be the four percentage numbers.
```{r chunk3}
```
Let's check the percentage numbers add up to 100.
Copy sum(pop_percentage) into code chunk 4 and click on the little arrow on
the right of the chunk. What number does R print for you? Is it 100?
```{r chunk4}
```
Now we will save the blood group and percentages data in a table (known as a tibble in R) called 'mydata'. This is so that the two variables can be combined into a single data table.
Copy the following code into R chunk5.
mydata <- tibble(blood_group, pop_percentage)
```{r chunk5}
```
Lets check that 'mydata' has the correct information in it.
Type mydata into code chunk6 and then click on the arrow on the right. Does the computer show you a little table with the correct information in?
```{r chunk6}
```
## Drawing a bar chart using our blood group data
Now we have entered the data and checked that the data is correct we can tell R to plot a bar chart using these data.
Copy and paste the three lines of R code into chunk7 and click on the little arrow on the right.
ggplot(mydata) +
aes(x=blood_group, y=pop_percentage) +
geom_bar(stat = 'identity')
## What does this code mean?
- ggplot means 'draw me a graph'
- mydata means 'use the numbers in the mydata tibble'
- aes is short for aesthetics and means 'what I want to see is...'
- x=blood_group means 'plot blood group on the x axis'
- y=pop_percentage means 'plot percentage of people on the y axis'
- geom_bar means 'the graph I want is a bar chart'
- stat = 'identity' means 'make the bars as tall as the y values in the tibble'
```{r chunk7}
```
Check your bar chart. Are the bars black?
## Writing labels and title
Now lets alter the labels and add a title so that the bar chart is clearer.
Copy and paste the following R code into chunk8 then click on the little arrow on the right.
ggplot(mydata) +
aes(x=blood_group, y=pop_percentage) +
geom_bar(stat = 'identity')+
ylab('Percentage of the population')+
xlab('Blood group')+
ggtitle('UK Population Blood Groups')
You have added some more instructions for R. What do they mean?
ylab('Percentage of the population') means 'make this the label for the y axis'
xlab('Blood group') means 'make this the label for the x axis'
ggtitle('UK Population Blood Groups')means 'make this the title for the barchart'
Note how there is a plus sign after each command
```{r chunk8}
```
Check your bar chart. Are the labels correct? Is the title correct? Are the bars still black?
Well done!
## Changing the colour of the bars
Let's make it prettier.
Let's make the bars red.
Copy and paste the R code below into chunk9 and click on the little arrow on the right
ggplot(mydata) +
aes(x=blood_group, y=pop_percentage) +
geom_bar(stat = 'identity', fill = 'red')+
ylab('Percentage of the population')+
xlab('Blood group')+
ggtitle('UK Population Blood Groups')
```{r chunk9}
```
Are the bars red? Suggest what part of the code told R to make the bars red.
Let's make it even prettier. Copy and paste the following code into chunk10
ggplot(mydata) +
aes(x=blood_group, y=pop_percentage, fill = blood_group) +
geom_bar(stat = 'identity')+
ylab('Percentage of the population')+
xlab('Blood group')+
ggtitle('UK Population Blood Groups')
```{r chunk10}
```
What happened? Suggest what part of the code told R to colour the bars. How did R colour the bars?
Well done! You have successfully completed your second R lesson.
Now KNIT YOUR DOCUMENT for the final time. This is the version of your work that your teacher will mark.
## THE END
<div class="license">
**License and Citation:** You can use, modify, and adapt any of the lessons, but please include the following attribution: *RGirls Community. (2022, April 10). RGirls Lessons. Zenodo. [https://doi.org/10.5281/zenodo.6436861](https://doi.org/10.5281/zenodo.6436861)*
</div>