Baby Steps in Coding R

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.

Download .Rmd here

Download word document here

Download pdf here

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:

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:

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

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.
Blood Group O A B AB
Percentage of population 44 42 10 4
FALSE
[1] FALSE

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

FALSE
[1] FALSE

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.

FALSE
[1] FALSE

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?

FALSE
[1] FALSE

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)

FALSE
[1] FALSE

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?

FALSE
[1] FALSE

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?

FALSE
[1] FALSE

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

FALSE
[1] FALSE

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’)

FALSE
[1] FALSE

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’)

FALSE
[1] FALSE

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

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