![]() David holds a doctorate in applied statistics. His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. See our full R Tutorial Series and other blog posts regarding R programming.Ībout the Author: David Lillis has taught R to many researchers and statisticians. In the next blog post, we will look again at regression. ![]() By the way – lm stands for “linear model”.įinally, we can add a best fit line (regression line) to our plot by adding the following text at the command line: abline(98.0054, 0.9528)Īnother line of syntax that will plot the regression line is: abline(lm(height ~ bodymass)) Syntax: plot(x, y, type l, lty 1) lines(x, y, type l, lty 1) Parameters: x, y: coordinate vectors of points to join type: character indicating the type of plotting. After creating the matrix, now we will plot the lines and points. We see that the intercept is 98.0054 and the slope is 0.9528. Plotting line and point on graph with different color. Now let’s perform a linear regression using lm() on the two variables by adding the following text at the command line: lm(height ~ bodymass) Call: In the above code, the syntax pch = 16 creates solid dots, while cex = 1.3 creates dots that are 1.3 times bigger than the default (where cex = 1). Copy and paste the following code into the R workspace: plot(bodymass, height, pch = 16, cex = 1.3, col = "blue", main = "HEIGHT PLOTTED AGAINST BODY MASS", xlab = "BODY MASS (kg)", ylab = "HEIGHT (cm)") There are many different ways to use R to plot line graphs, but the one I prefer is the ggplot geomline function. We can enhance this plot using various arguments within the plot() command. We can now create a simple plot of the two variables as follows: plot(bodymass, height) Copy and paste the following code to the R command line to create the bodymass variable. Now let’s take bodymass to be a variable that describes the masses (in kg) of the same ten people. Copy and paste the following code to the R command line to create this variable. We take height to be a variable that describes the heights (in cm) of ten people. Today let’s re-create two variables and see how to plot them and include a regression line.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |