This lesson follows on from ‘Using R to draw the equation of a straight line graph - Part 1’ and demonstrates how to use R to plot straight line graphs (y=mx+c) with different intercepts and negative gradients followed by questions on the observed differences.
Plotting and finding the equations of straight line graphs y=mx+c
tidyverse
This is a worked example for you to follow.
We will show you how to plot the line graph for y=2x + 5.
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_point () +
geom_line (col='red')+
geom_vline (xintercept = 0, col='black')+
geom_hline (yintercept = 0, col='black')
We will now see what happens when you change the intercept c to a negative value.
Run the code in chunk2 by clicking on the little arrow on the right of the code chunk.
x <- seq(-4, 4) # sequence from -4 to 4
y1 <- 3*x+5
y2 <- 3*x-5
mydata <- tibble (x,y1,y2,y)
ggplot(mydata) +
geom_line (aes(x=x, y=y1), col='red')+
geom_line (aes(x=x, y=y2), col='blue')+
geom_vline (xintercept = 0, col='black')+
geom_hline (yintercept = 0, col='black')
Question: What is the same and what is different about these lines?
Answer:
Knit your document and check the output.
Draw these lines on a graph. Use the R code from chunk2 to help you. Remember to update the code with these new lines.
Question: What is the same and what is different about these lines?
Answer:
Now we will investigate m (the gradient). Draw these lines on a graph. Use R code from chunk2 to help you.
Run the code and knit the document.
Question: What is the same and what is different about these lines?
Answer:
Q1 In the equation y=mx+c, what happens when you change c?
Q2 What happens when you change m?
Write your answers here
Q1:
Q2:
Write down the equations of the following four lines on the graph below.
Knit the document to get a good view of the graph.
Write your answers here
Cyan line:
Red line:
Blue line:
Orange line:
KNIT YOUR DOCUMENT for the final time. This will be the version that your teacher will mark.
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