This lesson is part 1 of 2 and demonstrates how to use R to plot straight line graphs (y=mx+c) and to observe what happens when the value of m changes
Plotting straight line graphs y=mx+c
tidyverse
Knit your document often to check the output.
Make sure you do a final knit at the end of the lesson
This is a worked example for you to follow.
We will show you how to plot the line graph for y=2x + 5.
Run the code by clicking on the little arrow to the right of the code chunk.
x <- seq(from=-4, to=4, by=1) # sequence the x-axis from -4 to 4
y <- 2*x+5
mydata <- tibble (x,y)
ggplot(mydata) +
aes(x,y) +
geom_line (col='red')+
geom_vline (xintercept = 0, col='black')+
geom_hline (yintercept = 0, col='black')
Now close the image by clicking on the X to the right of the graph
Write your own code to draw the following graphs.
Use code chunk1 from the example above to help you.
Drawing more than one line on a graph helps us to compare the lines and see what is the same and what is different. Here is a worked example for four different lines.
x <- seq(-4, 4) # sequence from -4 to 4
y1 <- 3*x+0
y2 <- 3*x+1
y3 <- 3*x+2
y4 <- 3*x+3
mydata <- tibble (x,y1,y2,y3,y4)
ggplot(mydata) +
geom_line (aes(x=x, y=y1), col='red')+
geom_line (aes(x=x, y=y2), col='blue')+
geom_line (aes(x=x, y=y3), col='green')+
geom_line (aes(x=x, y=y4), col='grey') +
geom_vline (xintercept = 0, col='black')+
geom_hline (yintercept = 0, col='black')
Question: From the graph what looks the same and what looks different about these lines?
Answer:
Draw the following lines on a graph
Use code chunk5 from the example above to help you. Remember to update the R code with the new lines.
Run the code. Knit the document.
Question: What is the same and what is different about the lines?
Answer:
KNIT YOUR DOCUMENT for the final time. This will be the version that your teacher marks.
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